Jump to content

error getting data through PHP

Hi, 
I am fairly new to php and SQL. I had this working before with a different connect script but even when changing it back it stoped working again. 

So as the title states i am getting an error when trying tro retrieve data from my SQL database. 
 

Warning: mysqli_query() expects parameter 1 to be mysqli, resource given inD:\xampplite\htdocs\website\active.php on line 45error getting data

Active.php

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><!-- <!DOCTYPE html> --><html lang="en">		<head>		<META HTTP-EQUIV="refresh" CONTENT="15">		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">		<meta http-equiv="X-UA-Compatible" content="IE=edge">		<title>Yeronga SHS - Repair List</title>				<!-- Latest compiled JavaScript -->		<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>		<!-- Latest compiled and minified CSS -->		<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">		<link rel="stylesheet" type="text/css" href="./CSS/style.css">		<link rel="stylesheet" type="text/css" href="./CSS/modal.css">		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>			</head>		<body>		<div class="container">					<h1 style="text-align:center; font-weight:bold; color:white;"><img src="./Images/sitelogo.png" style="height:100px;"/>Yeronga State High School</h1>						<div class="" style="text-align:center">				<input style="background-color:#D6D6D6; border:2px solid yellow" type=button class="btn btn-default"onClick="parent.location='active.php'" value='Active Repairs'>				<a type="button"class="btn btn-default" href="#openModal" style="font-weight:bold;">Add Repairs</a>				<div id="openModal" class="modalDialog">					<div>						<a href="#close" title="Close" class="close">X</a>						<iframe src="/website/add_job-form.php"></iframe>					</div>				</div>				<input type=button class="btn btn-default"onClick="parent.location='#'" value='Repair Complete'>				<input type=button class="btn btn-default"onClick="parent.location='#'" value='Search Repair'>				<input type=button class="btn btn-default"onClick="parent.location='#'" value='Repair Archive'>			</div><?phpinclude ('connect-mysql.php');$sqlget = "SELECT * FROM repairs";$sqldata = mysqli_query($link, $sqlget) or die('error getting data');echo "<table class='table table-striped'>";echo "<thead>";echo "<tr><th>First Name</th><th>Last Name</th><th>Barcode</th><th>Asset ID</th><th  class='fault'>Fault</th><th>Status</th><th>Date</th></tr>";echo "</thead>";echo "<tbody>";while($row = mysqli_fetch_array($sqldata)) {	echo "<tr><td>";	echo $row['FirstName'];	echo "</td><td>";	echo $row['LastName'];	echo "</td><td>";	echo $row['Barcode'];	echo "</td><td>";	echo $row['Asset'];	echo "</td><td  class='fault'>";	echo $row['Description'];	echo "</td><td>";	echo $row['Status'];	echo "</td><td>";	echo $row['Date'];	echo "</td></tr>";}echo "</tbody>";echo "</table>";?></body></html<

connect-mysql

<?phpdefine('DB_NAME', 'yerongashs');define('DB_USER', 'root');define('DB_PASSWORD', '');define('DB_HOST', 'localhost');$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);?>
Link to comment
https://linustechtips.com/topic/369340-error-getting-data-through-php/
Share on other sites

Link to post
Share on other sites

try changing mysqli_query on line 45 to mysql_query

I am fairly sure I had tried that, I will check again when I get back on the PC.

 

Update: 

Changed  mysqli_query -->  mysql_query 

 

Now i get another error 

 

Warning: mysql_query() expects parameter 1 to be string, resource given in D:\xampplite\htdocs\website\active.php on line 45

 

Edited by Guest
Link to post
Share on other sites

I am fairly sure I had tried that, I will check again when I get back on the PC.

 

Update: 

Changed  mysqli_query -->  mysql_query 

 

Now i get another error 

 

 

Try reading the documentation for that function.

 

However, you should be using PDO (if you have to write your own code in PHP at all), not the deprecated and dangerous mysql extension.

 

And follow your own threads.

Link to post
Share on other sites

Try reading the documentation for that function.

 

However, you should be using PDO (if you have to write your own code in PHP at all), not the deprecated and dangerous mysql extension.

 

And follow your own threads.

Even if i do read the function, it makes no sence to me. I have to watch someone explain it to me like a lot of things i learn. I am more hands on than reading. according to w3schools there isn't anything wrongs with using SQLI and is up to the the programmer what one they choose. 

Link to post
Share on other sites

You can use mysqli or PDO. NOT mysql_

Mysql_ functions are eleven (11) fucking years old. If w3schools is saying to use that library, then they're even worse than I thought.

It is not up to the programmer. If someone writing for me tried to use that crap I'd take them out to the woodshed and paddle them.

Anyways. Your problem isn't really that you don't know how to use the mysql* stuff, it's that you don't actually understand php.

I'd recommend picking up a book on it and getting the basics. It's pretty easy to learn how to read the documentation too. The error you're seeing is because the mysqli functions expect a resource (ie a database connection variable that's been instantiated and connected to a database) to run the function against) though why you're not using the Native drivers and OOP, I'm not sure.

The mysql_ functions (and seriously please don't use these. Each time you do, God kills a puppy), expect a string as the first param, and their driver-specific resource as the second.

Not trying to sound like a douche.. I've just seen too much bad code.

--Neil Hanlon

Operations Engineer

Link to post
Share on other sites

Even if i do read the function, it makes no sence to me.

 

Hope I don't sound harsh, but you need to go back to square one. If you cannot understand what your problem is after reading the documentation for a basic function call you are in way over your head.

 

I usually recommend this online Python book. It focuses on the skills you need to be a good programmer, not just going through the same old rehash of what functions and variables are.

 

Back to your question; look at the documentation for the function. What are the arguments that it requires? What are you passing in? I think you'll find that there is a discrepancy. And again, you should be using mysqli at a minimum. It's important to get in the habit of adhering to best practices, like not using out-dated, insecure code.

Link to post
Share on other sites

You can use mysqli or PDO. NOT mysql_

Mysql_ functions are eleven (11) fucking years old. If w3schools is saying to use that library, then they're even worse than I thought.

It is not up to the programmer. If someone writing for me tried to use that crap I'd take them out to the woodshed and paddle them.

Anyways. Your problem isn't really that you don't know how to use the mysql* stuff, it's that you don't actually understand php.

I'd recommend picking up a book on it and getting the basics. It's pretty easy to learn how to read the documentation too. The error you're seeing is because the mysqli functions expect a resource (ie a database connection variable that's been instantiated and connected to a database) to run the function against) though why you're not using the Native drivers and OOP, I'm not sure.

The mysql_ functions (and seriously please don't use these. Each time you do, God kills a puppy), expect a string as the first param, and their driver-specific resource as the second.

Not trying to sound like a douche.. I've just seen too much bad code.

  

Hope I don't sound harsh, but you need to go back to square one. If you cannot understand what your problem is after reading the documentation for a basic function call you are in way over your head.

 

I usually recommend this online Python book. It focuses on the skills you need to be a good programmer, not just going through the same old rehash of what functions and variables are.

 

Back to your question; look at the documentation for the function. What are the arguments that it requires? What are you passing in? I think you'll find that there is a discrepancy. And again, you should be using mysqli at a minimum. It's important to get in the habit of adhering to best practices, like not using out-dated, insecure code.

thanks to all the replies. I will have a look into the reading material you have suggested. it's not so much that I wouldn't understand what the stuff is I get confused and find it hard to process how some things are written. I always have and that's my biggest road block with anything.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×