Php is not printing database information - php

I just set up a mysql data base, although my php isn't printing out the information that I want printed.
Here's what my code looks like (I've already connected to mysql and created a database successfully, I just need it to print out what I had inserted into it):
// will insert into the table
mysqli_query ($con, "INSERT INTO alarms(Title, Description, DT)
VALUES(getSucked, Agha will smd, 2013-08-05 00:15:12)");
//will print out what we just insert into the table, ie agha smd's
$result = mysqli_query($con,"SELECT * FROM alarms");
while ($row = mysqli_fetch_array($result))
{
echo $row['alarmID'] . " <br> " . $row['Title'] . " <br> " . $row['Description'] . " <br> " . $row['DT'];
}
//closing connection
mysqli_close($con);
echo "<br><br>connection has been closed";
The warning/error I keep getting is:
"Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in B:\DropBox\Dropbox\EbadlyAgha\Ebad\reminders.php on line 83"
Line 83 is where the while loop begins.

Your query failed to run. mysqli_query will return false if the query does not execute, hence the warning of mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given. You should always check the return value when interacting with the database, to make sure you are aware of what's going on. PHP will happily coerce many types into other types silently, so you have to be very proactive in your error checking.

mysqli_query returns FALSE on failure.
try
if ($result) {
while ($row = mysqli_fetch_array($result))
{
echo $row['alarmID'] . " <br> " . $row['Title'] . " <br> " . $row['Description'] . " <br> " . $row['DT'];
}
}

Change your Insert query to
mysqli_query ($con, "INSERT INTO alarms(Title, Description, DT)
VALUES('getSucked', 'Agha will smd', '2013-08-05 00:15:12')");
Your Insert query will fails when you gave the spaces.Try to put them in quotes..

Use
mysqli_query ($con, "INSERT INTO alarms(Title, Description, DT)
VALUES(getSucked, 'Agha will smd', '2013-08-05 00:15:12')");
and try to echoed query and run into phpmyadmin.because your value is with space so you have to use it in ''.

Related

MySQL fetch row and undefined variable error

I keep getting the following errors. The $query produces a 1300 result list. When I run echo $query I get the following MySQL error:
[25-Aug-2016 21:38:32 America/New_York] PHP Warning: mysql_fetch_row() expects parameter 1 to be resource, null given in song.php on line 285
[25-Aug-2016 21:38:32 America/New_York] PHP Notice: Undefined variable: song_hash in song.php on line 292
$query = "select " . $query_data . " from " . $query_tables . " where " . $query_where;
//echo $query;
$result = mysql_query($query,$database);
while($row = mysql_fetch_row($result)){
$key = $row[0];
$song_hash[$key] = ($song_hash[$key] + 1);
}
$largest = max($song_hash);
In order to help you the contents of $query_data, $query_tables and $query_where needed. You can also print the error with mysql_error(). More likely you will be able to find out the solution from that output.
http://php.net/mysql_error
when you run echo query and you get those errors it simply means you don't have a query..
try hardcoding the query for debugging,
also you need to initialize your song_hash array

PHP variable is not an mysqli object

I have the next code (compilated in NetBeans)
$query = 'INSERT INTO grupos(nombre, descripcion)'
. ' VALUES ("' . utf8_decode($_POST['name']) . '", "' . utf8_decode($_POST['desc']) . '")';
$result = $con->query($query) or exit("Error: " . $con->errno . ": " . $con->error);
if ($result->affected_rows > 0) {
Why I got this: "Trying to get property of non-object "
if when I made a echo $result; display a '1'?
From the documentation of mysqli::query():
For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
Since your query is INSERT, not SELECT, it doesn't return a mysqli_result, it just returns TRUE. You should use $con->affected_rows instead of $result->affected_rows.

Having trouble updating postgressdb using update clause

I'm having trouble updating my postgressdb using update clause where itemid = "$_get['itemid'];
here's my sql code but it returns Warning: pg_query(): Query failed: ERROR:
$sql="UPDATE tbl_item SET itemname='".$_POST['ItemName']."', highqntythreshold='".$_POST['HQThreshold']."', lowqntythreshold='".$_POST['LQThreshold']."', qntyperunit='".$_POST['QPUnit']."', itemtype='".$_POST['IT']."', description='".$_POST['Description']."', WHERE itemid='". $_GET['itemid'] . "';";
$iteminfo = pg_query($sql);
and it also returns "Warning: pg_affected_rows() expects parameter 1 to be resource, boolean given in D:\Wamp\wamp\www\Php\CTea\UpdateItem.php on line 303"
if(pg_affected_rows($iteminfo)==1)
{
$msg = "Successfully added new Item, ".ucfirst($_POST['ItemName'])."!";
}
else
{
$msg = "Error: in saving Item data!...";
}
i think i messed up something but can't figure it out where and what i messed up.
The problem is (at least) in this part:
$_POST['Description']."', WHERE itemid='". $_GET['itemid'] . "'
There is a comma before the where, so you want:
$_POST['Description']."' WHERE itemid='". $_GET['itemid'] . "'
In general, though, you should just print out the query string after variable substitution. About 98% of the time, the error is obvious and you can fix it quickly.

What defines the PHP error: Call to a member function fetch_object() on a non-object

I have been wondering on this question for a while. I have two PHP programs that are almost exactly identical except for the fact that on of them is a function and the other isn't. Furthermore one works and the other send back the error:
Call to a member function fetch_object() on a non-object
I fixed the one that wasn't working by omitting the variables and inserting definitive strings and adding $con->errno instead of mysqli_errno. However, when I replaced the strings with the variables again the problem returned.
So, my question is: what causes this error and how would I fix it. Also, why is the error coming up in the second code and not the first?
The First Code (works)
<?php
$con = new mysqli("database info");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$stmt = $con->query("SELECT coordinator, announcements, description, comments, picture FROM Class_data WHERE class_year = '" .$class. "';");
$result_row = $stmt->fetch_object();
$coordinator = $result_row->coordinator;
$announcement = $result_row->announcements;
$description = $result_row->description;
$comments = $result_row->comments;
$picturepath = $result_row->picture;
mysqli_error($con);
mysqli_close($con);
if ($picturepath == "")
{
$picturepath = "../images/AlumnLogo.png";
}
?>
Second Code (doesn't work)
<?php
function fetch($page, $content1, $content2, $content3)
{
$con = new mysqli("database info");
if ($con->errno)
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$stmt = $con->query("SELECT '" .$content1. "' , '" .$content2. "' , '" .$content3. "' FROM '" .$page. "';");
$result_row = $stmt->fetch_object();
$content = array();
$content[0] = $result_row->body;
$content[1] = $result_row->calendar;
$content[2] = $result_row->announcements;
$output = implode("--",$content);
mysqli_error($con);
mysqli_close($con);
return $output;
}
?>
Thanks alot!
You are quoting your column names using single quotes. You cannot do that, you need to quote table- and column names using backticks (in case of reserved words, spaces, etc.) and only (non-integer...) values need to be quoted using single or double quotes.
Change your code to:
$stmt = $con->query("SELECT `" .$content1. "` , `" .$content2. "` , `" .$content3. "` FROM `" .$page. "`;");
^ All these
By the way, I assume you are using a white-list for your table- and column names. If not, you should to avoid sql injection.
It is also a good idea to add error handling to your database calls. An easy way using mysqli, is to put mysqli_report(MYSQLI_REPORT_STRICT); at the start of your script. This will cause mysqli to throw exceptions so that you do not have to check for individual errors on each database call.
The error message simply means that you try to call a method on something that is not an object. The problem is that $stmt is not an object!
This happens because there is an error in your SQL Statemetn. $con->query() retuns false in such a case. Therefore, what you try to execute is something like false->fetch_object();. And false isn't an object...
Try to print your generated SQL Statement. It will probabely contain some syntax errors. Fix those, and it will work

Basic PHP/MySQL Error with mysql_fetch_array()

The error I get:
...mysql_fetch_array() expects parameter 1 to be resource, boolean given...
awayid is in the address bar properly. I can print it out just fine, but for some reason the following code gives me the above error.
$result = mysql_query("select * from team where id=" . $_GET['awayid']);
$row = mysql_fetch_array($result);
EDIT Tried the mysql_error(). It seems I forgot to select a database... however, even why I use mysql_select_db('gamelydb'); I still get the mysql error No database selected
Your query is failing... Therefore $result is set to false.
$result = mysql_query("select * from team where id=" . $_GET['awayid']);
var_dump($result); // bool(false)
Call mysql_error() to get the error message for your query:
echo mysql_error();
Your query is failing and returning a boolean FALSE. Try this:
$result = mysql_query("select ...") or die(mysql_error());
^^^^^^^^^^^^^^^^^^^^^^---- add this
This will kill the script and show you the exact reason the query is failing.
mysql_query() returns false if the query is unsuccessful, i.e. an error occured. That is why you need to check $result for being false first.
Use mysql_error() to output the error.
You need to be sure there is results from your query :
while ($row = mysql_fetch_array($result)) {
// echo $row[] ... ;
}
First of all, your query is very open to SQL injection attacks. Do not directly insert anything from $_GET or $_POST (or really anywhere) into your query. At the minimum, use mysql_real_escape_string on the variable.
mysql_query is returning false becuase there is something wrong with the query. You can use mysql_error to see what the last reported error is.
if ($result = mysql_query("select * from team where id='" . $_GET['awayid']) . "'") {
$row = mysql_fetch_array($result);
}
else {
echo mysql_error();
}
Anyway...you know that writing a $_GET parameter right into the SQL query is very very bad? Try it with PHP Data Objects.
Did you try and search around first Tory, we answer these questions over and over again, next time please search around.
The reason why this error occurs is because your running a query with mysql_query that fails, because it fails it returns false, you then pass the value of false to mysql_fetch_array, it's like doing mysql_fetch_array(false)
You need to make sure that mysql_query is successful:
try something like this:
if(false !== ($result = mysql_query("select * from team where id=" . $_GET['awayid'])))
{
$row = mysql_fetch_array($result);
}else
{
die("Query has failed: " . mysql_error())
}

Categories