getting warning while fetching rows from mysql using php - php

It seems to be duplicate question, but I tried all the answers, which has been already posted.
this is my code
include"db.php";
//$poster_id=1;
$sql = "SELECT pi.full_name,pi.poster_img,pi.poster_tag,
p.post_title,p.post_description,p.post_tag,
p.post_snap
FROM POSTS p JOIN POSTER_INFO pi ON p.poster_id=pi.auth_id";
$result = mysql_query($sql) or die(mysql_error());
echo mysql_error();
$msg=" ";
while($row = mysql_fetch_array($result)) {
$msg_footer="<div class='post_footer'>";
$msg_footer=$msg_footer."<div class='post_img'>";
$msg_footer=$msg_footer."<img src='". 'data:image/jpeg;base64,' . base64_encode( $row[6] ) ."' width='330' height='130' /></div>";
$msg_footer=$msg_footer."<div class='post_author'><div class='author_img'><img src='". 'data:image/jpeg;base64,' . base64_encode( $row[1] ) ."' width='80' height='80' /></div>";
$msg_footer=$msg_footer."<div class='author_info'><b>". $row[0] ."</b><br/><span style='font-size:11px'>". $row[2] ."</span><br/><span style='font-size:10px'>1 Post</span></div></div></div>";
$msg .= " <li><br/><span class='post_title'>". $row[3] ."</span><br/><br/><span class='post_description'>" . $row[4] . "</span></li>";
$msg .=$msg_footer; //WARNING POINTING TO THIS LINE
}
I'm getting warning, which pointing last line of while loop;
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\labs\load_data.php on line 40
My sql query is correct, because, I tried to execute it in sql editor and in php page.
like this
<?php
$sql = "SELECT pi.full_name,pi.poster_img,pi.poster_tag,
p.post_title,p.post_description,p.post_tag,
p.post_snap
FROM POSTS p JOIN POSTER_INFO pi ON p.poster_id=pi.auth_id";
$result = mysql_query($sql) or die(mysql_error());
echo mysql_error();
while($row = mysql_fetch_array($result)) {
?>
<tr>
<td><?php echo $row[0]; ?></td>
<td><?php echo $row[2]; ?></td>
<td><?php echo $row[3]; ?></td>
</tr>
<?php }
?>
In this case, I'm not getting any warning.

I'm pretty sure that the error is not triggered by the code that you have posted.
Check the return value of mysql_query() at all positions in the code, like this:
if(mysql_query(...) === FALSE) {
die(mysql_error());
}
I see you are already doing it in the code you have shown, but the warning will be triggered at another point in the code I assume.
Another explanation might be, that you overwrite $result somewhere in the code - but this is not shown as well.
General advice: Don't use mysql_* functions for new code. They are deprecated. Use PDO or mysqli_* instead

I think your query is failing, so producing a boolean FALSE instead of a query resource. But it's very strange that the second one runs fine..

Related

Oracle Database won't output in my PHP table

I'm trying to output the data from a specific table within my Oracle SQL Developer database using a PHP form. I had some experience with this concept in the past using MySQL but I'm struggling to understand the differences between integrating it using Oracle specific syntax now.
My code is ideally laid out but when I run the code I get an error regarding the OCI_FETCH_ARRAY parameters, which I'm unsure on how to solve.
My Code
<?php
include("../connection.php");
error_reporting(E_ALL);
$sql = 'SELECT * FROM ALGORITHM';
$stid = oci_parse($conn, $sql);
echo "<table border='1'>";
echo "<tr> <th>ID</th> <th>Name</th> <th>Description<th> </tr>";
while($row= oci_fetch_array($stid, $sql))
{
echo "<tr>";
echo "<td>" . $row ['Algorithm_ID'] . "</td>";
echo "<td>" . $row ['Algorithm_Name'] . "</td>";
echo "<td>" . $row ['Algorithm_Role'] . "</td>";
echo "</tr>";
}
echo "</table>";
oci_free_statement($stid);
oci_close($conn);
?>
The Error I keep getting
Warning: oci_fetch_array() expects parameter 2 to be integer, string
given in
/studenthome.hallam.shu.ac.uk/STUDENTHOME8/1/b5040831/public_html/ADM/bin/php/entities/database.php
on line 12
I get that it's asking for an integer, but why? In the MySQL concept you simply outline the connection string it was never an issue.
Can anyone help ?
Calling oci_fetch_array() ( from http://php.net/manual/en/function.oci-fetch-array.php) should be called like
while($row= oci_fetch_array($stid))
The second parameter is the mode - i.e. OCI_ASSOC.
Update: when using oci_parse(), this doesn't actually execute the parsed statement, you need to do...
oci_execute($stid);
So your code would be something like...
$sql = 'SELECT * FROM ALGORITHM';
$stid = oci_parse($conn, $sql);
oci_execute($stid);
echo "<table border='1'>";
echo "<tr> <th>ID</th> <th>Name</th> <th>Description<th> </tr>";
while($row= oci_fetch_array($stid, OCI_ASSOC))
(Look at http://php.net/manual/en/function.oci-parse.php for code examples)

Using Foreach for website. I want a hit counter using PHP & MSQL

So I'm creating a piano library website for my younger cousin.
Currently I use the foreach function to display all the data from my database. Now, this works great and I managed to get a few features working, but one I'm having trouble with is a "counter".
Super easy concept, except the fact that I'd like a counter for every single entry.
By "counter" I mean that after clicking a link, it would add a +1 to the count. So that each link would have "Visited 100 times" or "Visited 34 times", etc.
I have tried the following:
if($mysqli){
$result = mysqli_query($mysqli,"SELECT * FROM testtable ".$orderbyfilter);
$rows = $result->fetch_all(MYSQLI_ASSOC);
foreach($rows as $row) {
echo "<tr id='entry'><td>";
echo ucwords($row['name']);
echo "</td><td align='center'>";
echo 'url';
echo "add hit:";
echo "<a href='?action=callfunction'>Click</a>";
//current counter script
if(isset($_GET['action']) && $_GET['action'] == 'callfunction'){
$hitcount = $row['hitcount'] + 1;
$id = $row['id'];
// why doesn't this work?
$sql="UPDATE testtable SET hitcount='$hitcount' WHERE id='".$id."'";
$result=mysqli_query($con,$sql);
}
echo "</td><td align='center'>";
echo $row['level'];
echo "</td><td align='center'>";
echo $row['hitcount'];
echo "</td></tr>";
}
mysqli_close($mysqli);
} else {
echo "table did not correctly display!";
}
Obviously the method:
$sql="UPDATE testtable SET hitcount='$hitcount' WHERE id='".$id."'";
Doesn't work, as when I click the link, it updates all entries with the same hit count. However when I change it to:
$sql="UPDATE testtable SET hitcount='$hitcount' WHERE id='2'";
It works perfectly, where it only modifies hitcount for the row with id=2.
Obviously the problem has to do with the "foreach" and setting $row[id] as a variable, but honestly I could use some help.
Does it have something to do with variable of variables? I have no clue. Any help is appreciated.
Here's what worked for me. You can modify it as per required.
What needed to be done was to pass an additional GET parameter for the related "id" in the URL, then pass that variable for it in the WHERE clause.
A header was also added in order to automatically redirect once a related URL to the given row has been clicked and show the results and to prevent the following warning:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in...
Sidenote: Read the comments left in the code below for additional information.
<?php
ob_start(); // keep this, it's for the header
$DB_HOST = 'xxx'; // Replace
$DB_USER = 'xxx'; // with
$DB_PASS = 'xxx'; // your
$DB_NAME = 'xxx'; // own
$Link = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($Link->connect_errno > 0) {
die('Connection failed [' . $Link->connect_error . ']');
}
// Your original query. You can replace it if needed.
// $result = mysqli_query($Link,"SELECT * FROM testtable ".$orderbyfilter);
$result = mysqli_query($Link,"SELECT * FROM testtable");
while ($row = mysqli_fetch_array($result)) {
echo "<tr id='entry'><td>";
echo "ID: " . $row['id'];
$var = $row['id']; // used for the URL
echo "<br>";
echo $row['hitcount'];
echo ucwords($row['name']);
echo "</td><td align='center'>";
echo 'url';
echo " Add hit: ";
echo "<a href='?action=callfunction&id=$var'>Click</a> ";
if(isset($_GET['action']) && $_GET['action'] == 'callfunction'){
// Used for the query and escape the URL
$var = mysqli_real_escape_string($Link, $_GET['id']);
$sql="UPDATE testtable SET hitcount = hitcount +1 WHERE id='".$var."'";
$result=mysqli_query($Link,$sql);
// Added a header, otherwise the old method will generate the following warning
// Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in...
header("Location: http://www.example.com/your_counter_file.php");
exit; // Keep this in order to stop further execution
// ^ Change the url in header above to reflect your Web site's address
} // Closing brace for isset
echo "</td><td align='center'>";
echo $row['level'];
echo "</td><td align='center'>";
echo $row['hitcount'];
echo "</td></tr>";
} // Closing brace for while loop
mysqli_close($Link);
Just change your query like this to update the counter
$sql="UPDATE testtable SET `hitcount`=hitcount+1 WHERE id=".$id;
Don't use single quotes around value of counter since its a integer

Add database fields according to id php [duplicate]

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();
}
?>

Trying to pass a student key from a html form to a php file to scan a database

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/

PHP+mySQL trouble. supplied argument is not a valid MySQL result resource [duplicate]

This question already has answers here:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result [duplicate]
(6 answers)
Closed 9 years ago.
Please look below for edit
Hi, everyone. My error is on my while loop. I've had to add a # to it to make it stop giving me the warning. It works just fine. Everything updates perfectly. But, rather than kludge it, I'd really like to not to have to hide my errors.
I just have no idea what is wrong with it. Like I said, my while loop (line 113) is giving me this:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/xx/xxxxxxx/xxx/admin/price_chng.php on line 113
Here's the code.
if($manu < 15){
$row_count = 0;
$query = "SELECT * FROM `phone_models` WHERE `manufacture_id`=" . $manu . ";";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
if($row_count < 5){
echo ("<form action='price_chng.php' method='post' name='newprice' id=" . $row['id'] . ">");
echo "<td>" . $row['label'] . "<br>";
echo("<input name='newprice' type='text' id=" . $row['id'] . " size='10' value=" . $row['buyback_price'] . "><br>");
echo("<input type='hidden' name='ud_id' value='$row[id]'>");
echo("<input name='doSave' type='submit' id='doSave' value='Save'></form></td>");
$row_count++;
}else{
$row_count = 0;
echo("</tr><tr>");
}
}
}else{
echo("</tr></td>");
}
Any help is appreciated! Thanks, guys!
EDIT: Updated the code. I took out the or die from my statement because I wanted the warning to stop. I forgot to put it back in. WHen I put it back in, I have this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
And, my code is:
if($manu < 15){
$row_count = 0;
$query = "SELECT * FROM `phone_models` WHERE `manufacture_id`=" . $manu . ";";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
if($row_count < 5){
echo ("<form action='price_chng.php' method='post' name='newprice' id=" . $row['id'] . ">");
echo "<td>" . $row['label'] . "<br>";
echo("<input name='newprice' type='text' id=" . $row['id'] . " size='10' value=" . $row['buyback_price'] . "><br>");
echo("<input type='hidden' name='ud_id' value='$row[id]'>");
echo("<input name='doSave' type='submit' id='doSave' value='Save'></form></td>");
$row_count++;
}else{
$row_count = 0;
echo("</tr><tr>");
}
}
}
else{
echo("</tr></td>");
}
It means exactly what it says. Your MySQL result (from mysql_query) is invalid. mysql_fetch_array expects a valid MySQL result resource, which is why an error is thrown.
I'd try changing:
$result = mysql_query($query);
To:
$result = mysql_query($query) or die(mysql_error());
The script should die and print out the MySQL error that is causing your query to fail.
Are you filtering your MySQL data? ($manu = mysql_real_escape_string($manu)) - Also you may want to wrap $manu in quotes.
..
In response to your comment, (although the mysql_error response can be cryptic) I'd try modifying the script as follows:
Replace:
$query = "SELECT * FROM `phone_models` WHERE `manufacture_id`=" . $manu . ";";
With (2 lines):
$manu = mysql_real_escape_string($manu);
$query = "SELECT * FROM phone_models WHERE manufacture_id = '" . $manu . "'";
I'd guess it's one of the following things causing your query to fail:
$manu is unfiltered, and could contain any number of things that would make the query fail
$manu is empty or null, and not wrapped resulting in (SELECT * FROM phone_models WHERE manufacture_id = )
Add or die(mysql_error()); to the end of the call to mysql_query() and see what it says.
There is nothing wrong with your SQL, except for possibly the semicolon at the end. The standard MySQL library for PHP does not support multiple SQL statements in a single SQL call, and therefore may be causing it to throw the warning.

Categories