Cannot select from database? - php

I'm trying to select from my MySQL database, from the table Lines. I made it really simple, it should simply show all rows in the database. However I'm getting an error on line 16 (I put an * next to it) that says: "mysql_fetch_array(): supplied argument is not a valid MySQL result resource"
$con = mysql_connect("...","...","mypass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname", $con);
$result = mysql_query("SELECT * FROM Lines");
while($row = mysql_fetch_array( $result )) ***************
{
echo $row['Text'];
echo "<br />";
}
mysql_close($con);
What am I doing wrong? Thanks

Lines is a reserved word in MySQL. Use backticks.
SELECT * FROM `Lines`

Add mysql_error() after mysql_query() to see if there's any error, I'm guessing table Lines doesn't exist:
$con = mysql_connect("copoetry.db.6945202.hostedresource.com","dbname","mypass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname", $con);
$result = mysql_query("SELECT * FROM Lines");
echo mysql_error(); die;
while($row = mysql_fetch_array( $result )) ***************
{
echo $row['Text'];
echo "<br />";
}
mysql_close($con);

Related

Mysql COUNT function not working

I have a database called articles and I just wanna print out the amount of rows in my database. This is my code:
<?php
$con = mysql_connect('link', 'database', 'password');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
$result = mysql_query("SELECT COUNT(*) FROM articles");
$row=mysql_fetch_array($result)
?>
<p>The amount of rows is <?php echo $row ?>.</p>
When I test this code, is just gives me 'The amount of rows is .' as output. What am I doing wrong?
Edit: This is my full code now, but it still doens't work:
$con = mysqli_connect('url', 'database', 'password');
if (!$con) {
die('Could not connect: ' . mysqli_error());
}
$result = mysqli_query($con, "SELECT COUNT(URL) AS row_count FROM articles");
if (!$result ) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysqli_error($con);
exit;
}
$row=mysqli_fetch_array($result)
?>
<p>The amount of rows is <?= $row['row_count'] ?>.</p>
The error: DB Error, could not query the database MySQL Error: No database selected
You're trying to print the row.
Use echo $row[0] instead
Also, as it's printing nothing, I suspect your query is failing, as FALSE will be echoed as nothing in PHP.
Sidenote: mysql_* is deprecated, consider using PDO instead.
EDIT:
full code below
<?php
$con = new mysqli('url', 'database', 'password', 'db_name');
if (!$con) {
die('Could not connect: ' . mysqli_error());
}
$result = $con->query("SELECT COUNT(*) AS row_count FROM articles");
if (!$result) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . $con->error;
exit;
}
$row = $result->fetch_assoc();
?>
<p>The amount of rows is <?= $row['row_count'] ?>.</p>
You are echoing an array (which should give you error). Instead you can set alias for COUNT:
$result = mysqli_query($con, "SELECT COUNT(*) AS Total FROM articles");
And echo it like:
<p>The amount of rows is <?php echo $row['Total'] ?>.</p>
As others said, you're trying to print the array (hence the fetch_array), when you need to print something in the array.
Also, mysql_* is depreciated, I use mysqli_* which is very similar and easy to switch to.
Also, instead of *, try a fieldname in the count. I usually use the index, or primary key or something.
<?php
$link = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
if (mysqli_connect_errno($link)) {
echo 'Could not select database:' . DB_NAME;
exit;
}
$result = mysqli_query($link, "SELECT COUNT(fieldname) AS row_count FROM articles");
if (!$result ) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysqli_error($link);
exit;
}
$row=mysqli_fetch_array($result)
?>
<p>The amount of rows is <?= $row['row_count'] ?>.</p>

PHP Disable round_up

How can I disable the round up in my PHP script. If I sum up something it only displays the rounded Number.
<?php
$con=mysqli_connect("localhost","XXXX","XXXX","XXXX");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM energrid WHERE ID = 1 ");
while($row = mysqli_fetch_array($result)) {
$wert = $row['Wert'];
}
$result = mysqli_query($con,"SELECT * FROM energrid WHERE ID = 3 ");
while($row = mysqli_fetch_array($result)) {
$wert1 = $row['Wert'];
}
mysqli_close($con);
echo "here we go.:";
echo $wert + $wert1;
?>
Ok, I guess I've found my mistake. The values are like 3,5 with a , instead of a .
How can I change that if these are values directly from the MySQL DB.?
1.You can use number_format function
2.optimize your code -there is no need of while loop to do this
<?php
$db = mysql_connect("localhost","username","password"); ///connect to mysql
if (!$db) {
// throw error if not connect
die("Database connection failed miserably: " . mysql_error());
}
$db_select = mysql_select_db("databasename",$db); ///select database
if (!$db_select) {
//database connection error
die("Database selection also failed miserably: " . mysql_error());
}
$sum=0;
$result = mysqli_query($db,"SELECT sum(wert) as sum FROM energrid WHERE ID in ('1','3') ");
$row = mysqli_fetch_array($result)
$sum =$row['sum'];
`enter code here`
echo number_format($sum,'number','.','');
//where number-no. of place you want to round off
?>
use str_replace() like this to replace the comma , in number by decimal point .
$a = "3,5";
str_replace(",",".",$a);
Try using a cast like this :
echo (int)$wert + (int)$wert1;
echo (float)$wert + (float)$wert1;

Strange behaviour trying to detect errors selecting a database

I have the following code in php:
<?php
$con = mysql_connect("localhost","john","john");
mysql_select_db("data", $con);
if (!$mysql_select_db) {
die ('Cannot use data : ' . mysql_error());
}
$result = mysql_query("SELECT * FROM test where huss='hussein'");
while ($row = mysql_fetch_array($result)) {
echo "ADSDAD";
echo "<br />";
}
if (!$con) {
die('ERROR Could not connect: ' . mysql_error());
}
mysql_close($con);
?>
I was able to connect to the MySQL server, but I can't seem to select the relevant database.
Being printed is the hard-coded text Cannot use data :, but no MySQL error message follows it. If the MySQL connection had failed then I would have expected a MySQL error message to appear after the hard-coded text.
What am I doing wrong?
The problem's here:
mysql_select_db("data", $con);
if (!$mysql_select_db) {
die ('Cannot use data : ' . mysql_error());
}
You don't have a variable $mysql_select_db, and it doesn't magically map to the result of the last time you called mysql_select_db.
So, instead:
$result = mysql_select_db("data", $con);
if (!$result) {
die ('Cannot use data : ' . mysql_error());
}
Or:
if (!mysql_select_db("data", $con)) {
die ('Cannot use data : ' . mysql_error());
}

How can I get a result from mysql_fetch_row (or mysql_fetch_array)

$sqlQuery = "SELECT * FROM allowedUsers WHERE UserID = '" . $kUserID . "'";
$result=mysql_query($sqlQuery, $db);
if(!result)
{
echo "Error running query <br>" . mysql_error();
exit;
}
while($row = mysql_fetch_array($result))
{
echo $row[2];
}
I run the SQLQuery in phpMyAdmin and I am getting a valid result (1 row)
the table (allowedUsers) has 6 fields
I can't get anything out of the DB.
Any help is appreciated.
if(!result) should be if(!$result)
According to PHP.net's documentation, you don't need to pass $db to mysql_query(). Take a look at the example code:
<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");
$result = mysql_query("SELECT id, name FROM mytable");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf("ID: %s Name: %s", $row[0], $row[1]);
}
mysql_free_result($result);
?>
It may be helpful to see your connection code, ensure you've selected a database, etc.

mysql_fetch_array() returns 'supplied argument is not a valid MySQL result resource'

I am trying the following code:
<?php
$link = mysql_connect('localhost', 'root', 'geheim');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$query = "SELECT * FROM Auctions";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
foreach($row as $field=>$value)
{
echo "$field: {$value} <br />";
}
}
mysql_close($link);
?>
And get this error:
Warning: mysql_fetch_array(): supplied argument is not a
valid MySQL result resource in
C:\Programme\EasyPHP 2.0b1\www\test.php on line 14
What am I missing?
You haven't selected a database - use mysql_select_db()
That would be something like:
<?php
$link = mysql_connect('localhost', 'root', 'geheim');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
die ('Error selecting database: '. mysql_error());
}
echo 'Using database successfully';
$query = "SELECT * FROM Auctions";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach($row as $field=>$value) {
echo "$field: {$value} <br />";
}
}
mysql_close($link);
?>
Your MySQL query possibly does not match any rows in the database.
Check the return value of mysql_query(), which returns "resource" on success and "false" on failure.
$query = "SELECT * FROM Auctions";
$result = mysql_query($query);
if ($result !== false) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach ($row as $field=>$value) {
echo $field . ':' . $value
}
}
} else {
// query returned 0 rows
}
As others also suggested, you can use mysql_error() to look if the query returns any mySQL errors
$query = "SELECT * FROM Auctions";
$result = mysql_query($query) or die(mysql_error());
so you'll see the error
Are you getting anything returned? If no results are found, mysql_query returns FALSE.
Check that before running fetch_array.

Categories