Mysql: Warning: mysql_fetch_array() expects parameter 1 to be resource - php

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

Related

In php-sql, I want to re-search within the first search result table.

In php-sql, I want to re-search within the first search result table.
This is the captured picture executed by my php-mysql code.
I want to use the button "Search below result" to gain the detailed result from the first search result table.
Then, "Search in below result" form action is another php code which has to hold the first result, and have the sql code that is as like
select uid, contents from datatable where contents like '%re-search word%'
and uid in (select uid from datatable where contents like '%first-search word%')
but I have a question and don't know how uid works.
How can i produce uid?
What is uid?
Where is uid information?
How can i use uid as like above the sql code?
Below is my first-search php code
<?php
$q = $_GET['q'];
$con = mysqli_connect('localhost','root','autoset','my_db');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM persons WHERE FirstName = '".$q."' ;
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
and How can i re-use the first-search word variable in the another php code?
Another php code is designed as like below
<?php
$q = $_GET['q'];
$p = $_GET['LastName'];
$con = mysqli_connect('localhost','root','autoset','my_db');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT uid, * FROM persons WHERE LastName = '".$p."' and select **uid** in (select uid from datatable where FirstName='".$q."') ;
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Above code, I think uid is important to recall the first search result.
But How can i get uid and set uid in php code or html code??
Please help me!
Q1: How can i produce uid?
A1: It should be a column in your table you created in mysql. Your create table statement would have looked something like:
CREATE TABLE persons (
uid INT(10) NOT NULL AUTO_INCREMENT,
FirstName CHAR(30) NOT NULL,
LastName CHAR(30) NOT NULL,
Age INT(3),
Hometown CHAR(40),
Job CHAR(40),
PRIMARY KEY (uid)
);
Q2: How can i produce uid?
A2: You won't have to. The AUTO_INCREMENT field will create itself for you when you insert an entry. doco for auto-increment here
Q3: What is uid?
A3: It likely stands for "user identification" which is a number unique to a user. No other user may have that number in their "uid" field.
Q4: Where is uid information?
A4: It's in your table
Q5: How can i use uid as like above the sql code?
A5: Providing that you created it in your "create table" statement like I mentioned in A1 then you should be able to access uid with the query you presented above but should look more like:
SELECT uid FROM persons WHERE LastName = '".$p."' AND FirstName ='".$q."';
OR if you want to run a test query with a name you know is in your database then something like below:
SELECT uid FROM persons WHERE LastName = 'timmy' AND FirstName ='tom';

Using two Tables to show SQL data on one Screen

I am still battelling to show data from two different tables on one screen. The tables both contain one common field name, all i want is to filter on that field name and be able to alter and display details from that line or lines based on that field name they share.
The two tables are:
members and recipients
In members it has a primary key member_id and it is unique. Then in recipients I created a member_id field that needs to link to the members table unique member_id as well.
The following code calls information from members table
<?php
$con = mysql_connect("localhost", "****", "****");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("stingin_epanic", $con);
$result = mysql_query("SELECT * FROM members WHERE member_msisdn='$slusername'");
echo "<table border='1'>
<tr>
<th>Membership</th>
<th>Number</th>
<th>Registration Date</th>
<th>End Date</th>
<th>Copy of ID</th>
</tr>";
while ($row = mysql_fetch_array($result)) {
echo "<td><center>" . $row['member_id'] . "</td>";
echo "<td><font color=blue>" . $row['member_msisdn'] . "</td>";
echo "<td><center>" . $row['asdate'] . "</td>";
echo "<td><center>" . $row['aedate'] . "</td>";
echo "<td><center>" . $row['attid'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
This works great and gives me what i need. I am also calling member_id here to show the primary key.
On same page i am trying to call information related to the member from another table using the following code. Note that the information is supposed to be linked to the member_id of the first code
<?php
$con = mysql_connect("localhost", "****", "****");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("stingin_epanic", $con);
$result = mysql_query("SELECT * FROM members a INNER JOIN recipients b ON member_id = member_id WHERE member_msisdn=$slusername");
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Number</th>
</tr>";
while ($row = mysql_fetch_array($result)) {
echo "<td><font color=blue>" . $row['recipient_name'] . "</td>";
echo "<td>" . $row['recipient_msisdn'] . "</td>";
echo "</tr>";
echo "<td><font color=blue>" . $row['recipient_name'] . "</td>";
echo "<td>" . $row['recipient_msisdn'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Using the second code i get no results as it is calling no information
Use mysqli instead of mysql functions
Use aliases in You SQL queries.
change this
"SELECT * FROM members a INNER JOIN recipients b ON member_id = member_id WHERE member_msisdn=$slusername"
on this
"SELECT * FROM members a INNER JOIN recipients b ON a.member_id = b.member_id WHERE member_msisdn=$slusername"
Maybe this will help.

Mysql Query Group By Display Using PHP

I want to display a table that looks like this from a Mysql query using PHP:
Vendor Name
Item Not to Drawing Item Defective Incorrect Item Received Other
9 2 3 5
Vendor Name
Item Not to Drawing Item Defective Incorrect Item Received Other
2 4 5 7
etc..
Here is my code....
<?php
$con = mysql_connect("localhost","xxx","xxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("qa", $con);
$result = mysql_query("SELECT Vendor, Sum(draw), Sum(defective), Sun(received), Sum(other) FROM qa_reports Group by Vendor Order by Vendor ASC");
echo "<table>
<tr>
<th>Item not to Drawing</th>
<th>Item Defective</th>
<th>Incorrect Item Received</th>
<th>Other</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Sum(draw)'] . "</td>";
echo "<td>" . $row['Sum(defective)'] . "</td>";
echo "<td>" . $row['Sum(received'] . "</td>";
echo "<td'>" . $row['Sum(other)'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Not sure how to get the above results formatted as such.
I've done this with CFML but am new to PHP and can not grasp how to list results in a table grouped by a field.
You need to alias your columns in order to reference them in your $row array.
SELECT vendor,
Sum(draw) AS draw,
Sum(defective) AS defective,
Sun(received) AS received,
Sum(other) AS other
FROM qa_reports ...
Then you can reference them like so:
$row['draw'];
...
SELECT Vendor, Sum(draw) AS sumDraw, Sum(defective) AS sumDefective ...
$row['sumDraw'] etc.

expects parameter

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

mysql_fetch_array() expects parameter 1 to be resource [duplicate]

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()

Categories