This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
“Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given” error while trying to create a php shopping cart
<?php
//connect to MYSQL
$con=mysql_connect("localhost","root","");
if (!$con)
{
die ('cannot connect:'.mysql_error());
}
//to show the original message
mysql_select_db("tracking", $con);
$result = "SELECT lat, lng, DATE_FORMAT(datetime,'%W %M %D, %Y %T') AS datetime FROM markers1 WHERE 1";
if (!$result) { // add this check.
die('Invalid query: ' . mysql_error());
}
echo "<table border='1'>
<tr>
<th>id No</th>
<th>lat Time</th>
<th>lng</th>
<th>datetime</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['lat'] . "</td>";
echo "<td>" . $row['lng'] . "</td>";
echo "<td>" . $row['datetime'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
It show me mysql_fetch_array() expects parameter 1 to be resource.
Can anyone help me .thx.
You're not running the query, it's only being stored in a string called $result. Here is the function you need: http://php.net/mysql_query
You aren't actually making a query...
Make the line...
$result = mysql_query("SELECT lat, lng, DATE_FORMAT(datetime,'%W %M %D, %Y %T') AS datetime FROM markers1 WHERE 1");
You need to call mysql_query to execute the query string. You also have a number of other problems with your code (the least of which is parameter binding.)
try this $result = mysql_query( "SELECT lat, lng FROM markers1 WHERE 1" );
You need to call the mysql_query() function first before you can call any fetch function such as mysql_fetch_array()
Related
Sorry to waste your time but I'm trying to store data from DB table into arrays and display in a table. I keep getting the same error. I've changed the "'s and removed variables. Still I get
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/a3656574/public_html/Home.php on line 41
<?php
if ($_POST['search_projects']){
$con= mysql_connect("host","username","password","a3656574_opacmin") or die ('Error: ' . mysql_error());
$sql= "SELECT * FROM searchedWords WHERE accessDate LIKE '%" . $_POST['search'] . "%' ORDER BY accessDate DESC";
$result= mysql_query($sql);
while($row= mysql_fetch_array($result))
{
$Date =$row['AccessDate'];
$Key=$row['keyWord'];
$Count=$row['count'];
echo "<tr>";
echo "<td>" .$Date ."</td> ". " <td>" . $Key. " </td>" . " <td>" . $Count. " </td>";
echo "</tr>";
}
}
?>
I don't know how to fix this. Can someone please help?
Mysql connection function receive 3 arguments (mysql_server, mysql_user, mysql_password)
and you should select database using mysql_select_db(database, connection_resource_id);
Also make sure your credential
Try:
$con= mysql_connect("host","username","password");
mysql_select_db("a3656574_opacmin",$con);
0) To begin, I would urge you to start using PDO instead of mysql_connect (and friends), as the latter is being deprecated. There's tutorial to help you start in this migration here.
1) I'm not sure what it is you're expecting the 4th argument to mysql_connect() to do for you. Per the PHP documentation, this should be a boolean value:
mysql_connect ([ string $server = ini_get("mysql.default_host") [, string $username = ini_get("mysql.default_user") [, string $password = ini_get("mysql.default_password") [, bool $new_link = false...
2) Check for error conditions before moving on to mysql_fetch_array():
$sql= "SELECT * FROM searchedWords WHERE accessDate LIKE '%" . $_POST['search'] . "%' ORDER BY accessDate DESC";
$result= mysql_query($sql);
if (! $result) {
// use something like mysql_error() to find out why it failed, log it, etc.
} else {
while( ... ) { ... }
}
Firstly I would like to recommend you to start using "mysqli". You can look for this here
and then you should first check if your credentials are right. If that is right got to phpmyadmin and try your query out there and see if its working fine. Maybe you are missing something. Good luck.
Please check your database credentials and then try with:
<?php
if ($_POST['search_projects'] && !empty($_POST['search']))
{
$con= mysql_connect("host.com","opacmin","password","opacmin") or die ('Error: ' . mysql_error());
$sql= "SELECT * FROM searchedWords WHERE accessDate LIKE '%" . $_POST['search'] . "%' ORDER BY accessDate DESC";
$result= mysql_query($sql);
while($row= mysql_fetch_array($result))
{
$Date =$row['AccessDate'];
$Key=$row['keyWord'];
$Count=$row['count'];
echo "<tr>";
echo "<td>" .$Date ."</td> ". " <td>" . $Key. " </td>" . " <td>" . $Count. " </td>";
echo "</tr>";
}
}
?>
I earlier made a database in mysql and now i am trying to list all the values from it in a table, but I get the following error Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean on line: while ($row=mysql_fetch_array($result))
Here is my code:
$con=mysql_connect("localhost","root","");
if (!$con) {
die("Error: " . mysql_error);
}
mysql_select_db("my_db",$con);
$result = mysql_query("SELECT * FROM Users");
echo "<table border='1'>
<tr>
<th>Username</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
<th>Email adress</th>
</tr>";
while($row=mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['emailadress'] . "</td>";
echo"</tr>";
}
echo "</table>";
mysql_close($con);
I read other similar question but diden't get an answer.
Your code doesn't evaluate the response from MySQL about selecting the database or running your query. The error indicates that your query didn't succeed (hence mysql_query returns FALSE) - which means one or both of the above didn't work.
Test for errors when you select the database and use die(mysql_error()); to see why these calls are failing.
Make sure this line is actually getting the rows from the table:
$result = mysql_query("SELECT * FROM Users");
Try doing an error check in that query:
$result = mysql_query("SELECT * FROM Users") or die(mysql_error());
Possibly your table is called "users" and not "Users" (note the caps), so change accordingly.
Regards,
Richi
here im getting problem with my join query..i dont know where is the problem whether my query is wrong or whatelse but the error its giving is
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\project\teacher\courses-list.php on line 46
heres my code please mend the code i cudnt find the problem.. :(
courses-list.php
<?php
if ($_SESSION["isteacher"])
{
$con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($dbname, $con);
$result = mysql_query("SELECT courses.id AS cid, courses.title, courses.description, courses.subjects-id, subjects.id AS sid, subjects.subjectname AS sname FROM courses, subjects WHERE (courses.subjects-id==subjects.id)");
echo "<table border='1'> <br />
<tr>
<th>ID:</th>
<th>Course Name</th>
<th>Description</th>
<th>Subject-ID</th>
<th>EDIT</th>
<th>DELETE</th>
</tr>";
while($row = mysql_fetch_array($result)) // this is the error line
{
echo "<tr>";
echo "<td>" . $row['cid'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $row['sname'] . "</td>";
echo "<td><a href='courses-edit.php?id=" . $row['id']."'>EDIT</a></td>";
echo "<td><a href='courses-delete.php?id=" . $row['id']."'>DELETE</a></td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
here is the error
$result = mysql_query("SELECT courses.id AS cid, courses.title, courses.description, courses.subjects-id, subjects.id AS sid, subjects.subjectname AS sname FROM courses, subjects WHERE (courses.subjects-id==subjects.id)");
should be
$result = mysql_query("SELECT courses.id AS cid, courses.title, courses.description, courses.subjects-id, subjects.id AS sid, subjects.subjectname AS sname FROM courses, subjects WHERE (courses.subjects-id=subjects.id)");
the error portion is
subjects WHERE (courses.subjects-id==subjects.id)");
here is the error ---------^^--------sould be =
also please avoid even dont use the mysql_* even the php manual show the message about that use the mysqli or PDO
I have the following code.
include("DBHeader.inc.php");
include("libs/ps_pagination.php");
$sql = "SELECT * FROM Products P, Manufacturers M WHERE M.sManufacturerCode='$LC' AND M.iManufacturerID=P.iManufacturerID";
$rs = mysql_query($sql);
echo $sql;
$pager = new PS_Pagination( $conn, $sql, 3, 4, null );
$rs = $pager->paginate();
$num = mysql_num_rows( $rs ) or die('Database Error: ' . mysql_error());
if ($num >= 1 ) {
echo "<table border='0' id='tbProd' class='tablesorter' style='width:520px;'>
<thead>
<tr>
<th>Product Code</th>
<th>Product Name</th>
<th> </th>
</tr>
</thead>
<tbody>";
//Looping through the retrieved records
while($row = mysql_fetch_array($rs))
{
echo "<tr class='prodRow'>";
echo "<td>" . $row['sProductCode'] . "</td>";
echo "<td>" . $row['sProductName'] . "</td>";
echo "<td><a href='ProdEdit.php?=" . $row['sProductCode'] . "'><img src='images/manage.gif' alt='Edit " . $row['sProductName'] . "' /></a></td>";
echo "</tr>";
}
echo "</tbody></table>";
}
else {
//if no records found
echo "No records found!";
}
And instead of it giving me the data from the table, it spits out on the screen:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/nyksys/www/regserver2/search_results.php on line 37
mysql_error() is actually returning nothing at all, so I'm very confused as to what the error is. The SQL when echo'd:
SELECT * FROM Products P, Manufacturers M WHERE M.sManufacturerCode='216E3ACAC673DE0260083B5FF809B102B3EC' AND M.iManufacturerID=P.iManufacturerID
I'm baffled here! Am I overlooking something simple here?
I've double checked my database information, I'm certain that isn't the problem.
EDIT- I'm following the tutorial Paginating Your Data with AJAX and Awesome PHP Pagination Class.
$sql = "SELECT * FROM Products P, Manufacturers M WHERE M.sManufacturerCode='$LC' AND M.iManufacturerID=P.iManufacturerID";
$rs = mysql_query($sql);
echo $sql;
$rs is a MySQL result resource that you could use with mysql_num_rows.
$pager = new PS_Pagination( $conn, $sql, 3, 4, null );
$rs = $pager->paginate();
Now it's not1!
$num = mysql_num_rows( $rs ) or die('Database Error: ' . mysql_error());
Oops!
1 Or, if it is, [a] you didn't show us that in your question, and [b] the original query was entirely pointless.
You are overwriting the $rs variable
My guess is whatever the PS_Pagination class is doing, it is not returning a MySQL resource. You are overwriting your $rs resource variable with that object, and it ceases to be a valid resource, even if your query succeeds.
$rs = mysql_query($sql);
echo $sql;
$pager = new PS_Pagination( $conn, $sql, 3, 4, null );
// Use a different variable than $rs here.
$rs = $pager->paginate();
$result = mysql_query("SELECT avg(r.rate) FROM rate r where ImgName='1'");
this php is not working.
Originally my code is
<?php
$con = mysql_connect("localhost","root","sql");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("photogallery", $con);
$result = mysql_query("SELECT avg(r.rate) FROM rate r ");
echo "<table border='0' cellspacing='5'>";
echo "<th> Average Rating </td>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td> " . $row['rate'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
the above is not showing any out put.
but modify code i.e. then its workin.
$result = mysql_query("SELECT r.rate FROM rate r ");
but i want to aggregate function
thanks in advance
you can use an alias:
SELECT avg(r.rate) AS rate_average
FROM rate r
WHERE ImgName='1'
and then output:
echo "<td> " . $row['rate_average'] . "</td>";
Your query is producing a scalar rather than a set of rows. If you want to get the average rate per item then you should do something like:
SELECT avg(r.rate) FROM rate r GROUP BY ItemIdColumn
And yes, if you want to fetch the value by column name, you should use an alias, like knittl mentioned.