This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 9 years ago.
I've been trying to output my database fields according to their empid, but I somehow can't. it gives me this error..
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:_webhost\Apache24\htdocs\eis\usercp.inc.php on line 16
<?php
$firstname = getuserfield('txtFname');
$lastname = getuserfield('txtLname');
echo 'Hello '.$firstname.' '.$lastname.'.';
$empid = getuserfield('empid');
$query = "SELECT type_of_leave,specific_reason,date_from,date_to,num_of_days FROM `hrf_leave` WHERE `empid` = '$empid' AND `formStatus` = 0";
$query_run = mysql_query($query);
echo "<table border=1>
<tr>
<th>Type of Leave</th>
<th>Specific Reason</th>
<th>Date From</th>
<th>Date To</th>
<th>Number of Days</th>
</tr>";
while($record = mysql_fetch_array($query_run)){ // line 16
echo "<tr>";
echo "<td>" . $record['type_of_leave'] . "</td>";
echo "<td>" . $record['specific_reason'] . "</td>";
echo "<td>" . $record['date_from'] . "</td>";
echo "<td>" . $record['date_to'] . "</td>";
echo "<td>" . $record['num_of_days'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
A boolean false is passed from mysql_query() since your sql statement $query is invalid. Therefor mysql_fetch_array() returns the above error.
Try this as $query, it will solve if it is a syntax error I hope.
$query= SELECT * FROM hrf_leave WHERE empid = '$empid' AND formStatus = 0";
See there might be 2 things :-
First the empid you used in the query might be integer and you are passing the value with in quotes.
Second the query is not returning any result. echo out the query and run it in the phpmyadmin or any mysql query browser.
Suggestion : Before using mysql_fetch_array use mysql_num_rows to check that if any rows are returned from the query.
Note : mysql_* functions are being depreciated. So avoid them
Before you do anything please try to not use mysql_ function its no more supported
You can use pdo. Bellow is how to connect to the datadabase using pdo from there you have to learn how to query pdo gud luck .
getMessage();
}
?>
Related
This question already has answers here:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result [duplicate]
(6 answers)
Closed 8 years ago.
Everything worked fine when using a localhost (data insertion and data retrieval worked well) I hosted our website today to develop coding for email activation and so on.
This site is for a computer store, there's a price list of several components categorized neatly. After the site was hosted I get the error:
"Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/a3270569/public_html/products.php on line 92"
I'm not sure why this happens.
Here's my code to retrieve data from one table:
<?php
$con = mysql_connect("hostname","username","pass"); //i changed these ;)
if (!$con){
die("Can not connect: ".mysql_error());
}
mysql_select_db("users",$con);
$sql = "SELECT * FROM intel";
$myData = mysql_query($sql,$con);
echo "<table id=test1 border=1 bgcolor=white>
<tr bgcolor=green>
<th>Processors</th>
<th>Price</th>
<th>Warranty</th>
</tr>";
while ($record = mysql_fetch_array($myData)){ //this is line 92 in my code
echo "<tr>";
echo "<td>" . $record['name'] . "</td>";
echo "<td>" . $record['price'] . "</td>";
echo "<td>" . $record['war'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
The query probably returns an error.
Add this below $myData = mysql_query($sql,$con);:
if (!$myData){
die(mysql_error());
}
This will display the error if the query is invalid.
Edit:
In order to also display errors from mysql_select_db(), replace mysql_select_db("users",$con) by this:
if (!mysql_select_db("users",$con)) {
die(mysql_error());
}
OK guys. I have a somewhat complicated issue with passing PHP variables into the mysql_query string.
The $_GET['date']; when passed will contain something like: 2015_01_07_1
I need to have the GET data passed into the table names using the $week variables.
<?php
$week= $_GET['date'];
$con=mysqli_connect("localhost","root","mypassword","beerhandpoker");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query
($con,
"SELECT games_brixx_gastonia_'$week'.rank, players_brixx_gastonia.name, games_brixx_gastonia_'$week'.points
FROM games_brixx_gastonia_'$week', players_brixx_gastonia
WHERE games_brixx_gastonia_'$week'.email = players.email
ORDER BY games_brixx_gastonia_'$week'.rank
LIMIT 20"
);
echo "<table>
<tr>
<th>Rank</th>
<th>Name</th>
<th>Points</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['rank'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['points'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Change the string literal to:
"SELECT games_brixx_gastonia_$week.rank,
players_brixx_gastonia.name,games_brixx_gastonia_$week.points
FROM games_brixx_gastonia_$week, players_brixx_gastonia
WHERE games_brixx_gastonia_$week.email = players_brixx_gastonia.email
ORDER BY games_brixx_gastonia_$week.rank
LIMIT 20"
You have to remove the ' characters;
It's going to the db as games_brixx_gastonia_'2015_01_07_1'.rank
Why do you put single quotes? It should work:
SELECT games_brixx_gastonia_{$week}.rank, players_brixx_gastonia.name, games_brixx_gastonia_{$week}.points
FROM games_brixx_gastonia_{$week}, players_brixx_gastonia
WHERE games_brixx_gastonia_{$week}.email = players.email
ORDER BY games_brixx_gastonia_{$week}.rank
LIMIT 20
Anyway, I'd rather advice you to use statement instead. Check it out:
http://php.net/manual/pt_BR/mysqli.prepare.php
Just remove the ' characters. Otherwise the query would try to get data from the table games_brixx_gastonia_'2015_01_07_1' and not games_brixx_gastonia_2015_01_07_1.
This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 9 years ago.
I got a little problem here.
I just exported my EasyPHPV13 databases into EasyPHPV14 on another computer.
When I try to run my php code I get this error :
mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in
The odd thing, it's that on the old computer it works, and on the new computer with EasyPHP V14, with imported databases and localweb data I get the error. I marked the code with /*error for the lines I get error at
My php code:
<?php
$con=mysqli_connect("localhost","root","root","site_db"); /*error*/
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM produse where categorie='1'"); /*error*/
echo "
<table border='1'>
<tr>
<th ALIGN=center>Denumire</th>
<th>Specificatii</th>
<th>Pret</th>
</tr>";
while($row = mysqli_fetch_array($result)) /*error*/
{
echo "<tr>";
echo "<td ALIGN=center>" . $row['denumire'] . "</td>";
echo "<td ALIGN=center>" . $row['specificatii'] . "</td>";
echo "<td ALIGN=center>" . $row['pret'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con); /*error*/
?>
Switch around the $con and the query.
http://www.php.net/manual/en/function.mysql-query.php
This question already has answers here:
How can I use PDO to fetch a results array in PHP?
(2 answers)
Closed 2 years ago.
I have a php script that selects data via mysql_, however recently I have been reading that PDO is the way to go and that mysql_ is becoming depreciated. Now I am converting that script to PDO.
My question is though, I am not using $_POST to select. I just want to select the entire table with all of its data so I enter this query :
$query = $dbh->prepare("SELECT * FROM students");
$query->execute();
$result = $query->fetchall(); // or you can just $result = $query as hakre proposed!
so then like I did with my old depreciated mysql_ version of the script I used the echo to echo a table with the data in it.
echo
"<table border='2'>
<tr>
<th>ID</th>
<th>A Number</th>
<th>First Name</th>
<th>Last Name</th>
<th>Why</th>
<th>Comments</th>
<th>Signintime</th>
</tr>"
;
foreach($result as $row)
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" .$row['anum'] . " </td>";
echo "<td>" . $row['first'] . "</td>";
echo "<td>" . $row['last'] . "</td>";
echo "<td>" . $row['why'] . "</td>";
echo "<td>" . $row['comments'] . "</td>";
echo "<td>" . $row['signintime'] . "</td>";
echo "<td> <input type=\"button\" value=\"Start Session\"onClick=\accept.php?id=" . $row['id'] . "&start=true></td>";
}
echo "</tr>";
echo "</table>";
now using this, I can not get a single output to my table.
My question is am I missing something from my select statements? Or am I not fetching any rows? Also I the connection settings set in another script called connect.php that is required by init.php (at the top of all of my pages)
Edit : 1
Edited the code so it now works, also adding a picture to show others how it should look! Hopefully some one can put this to some sort of use!
You are doing too much actually:
$query = $dbh->prepare("SELECT * FROM students");
$query->execute();
$result = $dbh->query($query);
The problematic line is:
$result = $dbh->query($query);
Check with http://php.net/pdo.query, the parameter is a string, actually the SQL string you already use above, not the result value of a PDO::prepare() call.
For your simple query you can just do:
$result = $dbh->query("SELECT * FROM students");
Or if you like to prepare:
$query = $dbh->prepare("SELECT * FROM students");
$query->execute();
$result = $query;
The later is some boilerplate if you want to insert variables into the query, that's why you prepare it.
The next problem is with the foreach line:
foreach($result as $row);
You are terminating the loop immediately because of the semicolon ; at the end. Remove that semicolon so that the following angle-bracketed code-block becomes the body of the foreach-loop.
Your code is wrong:
$query = $dbh->prepare("SELECT * FROM students");
$query->execute();
$result = $dbh->query($query);
After executing a prepared statement, you can just call fetchAll() on it:
$query = $dbh->prepare("SELECT * FROM students");
$query->execute();
$result = $query->fetchAll();
The rest of your code will work fine once you remove the semicolon after the foreach.
Basicaly having issues setting up a webpage which will taken in a student key entered by the user. This will then parse the student key to another file which will run it against a mysql backend to see what records this student already has. But can not get it working for the life of me please help I'm still a newb at this.
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("support_log", $con);
$result= mysql_query("SELECT student.first_name, student.surname, student.year_group, student.STKEY, student_log.issue
FROM `student` JOIN `student_log`
WHERE student.STKEY like '$_POST[stkey]'");
$result2 = mysql_query($result) or die("Error: " . mysql_error());
if(mysql_num_rows($result2) == 0){
echo("no records found");
} ELSE {
echo "<table border='1'>
<tr>
<th>First name</th>
<th>Surname</th>
<th>Year Group</th>
<th>Student Key</th>
<th>Issue</th>
</tr>";
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['First_Name'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['year_group'] . "</td>";
echo "<td>" . $row['stkey'] . "</td>";
echo "<td>" . $row['issue'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
mysql_close($con);
?>
After changing my where statement to:
WHERE student.STKEY like '$_POST[stkey]'");
I am no longer reciving errors from PHP but now recieving the error Query was empty which is part of my code to detect if there is no results. Though I have tested that query in phpmyadmin and it spits out results. From looking at the code does anyone have any solutions? I have also checked the parse by running an echo on the post command to ensure the data being entered was correct.
Edit: Got rid of the whole result2 check now throwing a:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\stkey_submit.php on line 24
Try $_POST['stkey'] instead of $_POST[stkey]
EDIT : if you use it in a query, it would be preferable to do :
$stkey = mysql_real_escape_string($_POST['stkey']);
$sql = "SELECT ....... like '$stkey'";
mysql_query($sql);
$result= mysql_query("SELECT student.first_name, student.surname, student.year_group, student.STKEY, student_log.issue
FROM `student` JOIN `student_log`
WHERE student.STKEY like " . $_POST["stkey"]);
How about storing the value of stkey on a variable before including it on the query?
$stkey = $_POST['stkey'];
$result= mysql_query("SELECT student.first_name, student.surname,
student.year_group, student.STKEY, student_log.issue
FROM `student` JOIN `student_log`
WHERE student.STKEY LIKE '%$stkey%'");
You might also want to use MySqli or PDO instead of the MySql database API. Take a look at this post from Nettuts: http://net.tutsplus.com/tutorials/php/php-database-access-are-you-doing-it-correctly/