what is wrong with this mysql code - php

$db_user="root";
$db_host="localhost";
$db_password="root";
$db_name = "fayer";
$conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn't connect to server");
// perform query
$query = 'SELECT * FROM posts';
$result = mysqli_query($conn, $query) or die ("Couldn't execute query.");
// use returned data
while($row = mysqli_fetch_assoc($result))
{
echo $row['title'];
}
I get in the browser: "mysql problem".
Help!
UPDATE
I have echoed the query. It shows SELECT * FROM posts and when I query manually it gets the rows.
I think it has something to do with mysqli. I think i should use mysql. Do u think I have incompatibility problems with mysqli?
i have echoed it. it shows SELECT * FROM posts. and when i query manually it gets the rows.
i think it has something to do with mysqli. i think i should use mysql. do u think i have incompatibility problems with mysqli?

You have empty WHERE clause. Remove it or add a search condition.

Change
$result = mysqli_query($conn, $query) or die ("Couldn't execute query.");
to
$result = mysqli_query($conn, $query) or die ("Couldn't execute query because: " . mysqli_error());
and you will know why the query is failing. Rule of thumb: Whenever you have a failed query, print it out and run it through phpmyadmin or some other raw-query executor and you will discover very quickly what the problem is.

Related

Providing Count from MySQL using PHP

I am trying to output a count of the number of results I have in a survey. The query itself ($sql) seems correct, because I do not get the error I have included, when I know that works. What isn't happening is the output. I have all my PHP tags and my database connection in place just fine, so I haven't included them here. They function just fine. What I need to know is how to get this outputted. This echoing for a count had worked for a previous PHP database set, but it is not working for this one.
My database has just ONE table, named survey. 'sur_cnt' is an auto-increment field that adds up whenever a new input is added to the database. My page output comes out blank, so I believe something is wrong with the echo, but I'm not sure what.
$sql = "SELECT COUNT(`sur_cnt`) FROM Survey";
$num = mysqli_query($db, $sql) or die('Error
querying database.');
$num_results = $result->num_rows ;
echo $num_results ;
try this query
because COUNT() using with groub by cluase
$sql = "SELECT * FROM Survey"; //remove count()
$result= mysqli_query($db, $sql) or die('Error querying database.');
$num_results = mysqli_num_rows($result) ;
echo $num_results ;
You need to use some form of fetch method to actually retrieve the count from your SQL statement - just the same as any other type of data from the database...
$sql = "SELECT COUNT(`sur_cnt`) FROM Survey";
$num = mysqli_query($db, $sql) or die('Error querying database.');
$row = mysqli_fetch_array($num) ;
echo $row[0] ;

Trouble querying database with php*fixed*

I am having trouble selecting my data from a database and displaying it. I have looked at tutorials and i still get the same error. Some help would be appreciated. The error i am getting is couldnt fetch result.
$sql = "SELECT * FROM data";
$result = mysql_query($sql) or die("couldnt fetch result");
if($result > 0){
while ($rows = mysql_fetch_array($result)){
$username = $rows['username'];
echo $username;
}
}
Just do that (assuming got it right connecting to DB, first thing to check !)
$sql = "SELECT * FROM `data`"; // data is a reserved keyword, protect it !!!
$result = mysql_query($sql) or die("couldnt fetch result"); // potentially diying here
if($result){
while ($row = mysql_fetch_assoc($result)){
$username = $row['username'];
echo $username;
}
}
If what you're getting is literally 'couldnt fetch result' it means your mysql_query() fails, and die statement takes over. Check your database connection.
I think the very simple problem is that you check if the $result is greater then 0. But you get an resource.
$conn = mysql_connect.......
$sql = "SELECT * FROM data";
$result = mysql_query($sql) or die("couldnt fetch result");
if($result){
while ($rows = mysql_fetch_array($result)){
$username = $rows['username'];
echo $username;
}
}
And if you see your die statement you have an error in your SQL Syntax. Its very short but its possible that your table doesn't exist in that database you're trying to connect. I hope you have a connect before and its not your complete code.
You use the old mysql functions. Its better to use MySQLi or PDO.
And DATA is a reserved keyword its possible that you get problems if you use it in your query. Rename your table in prefix_data for example.
https://dev.mysql.com/doc/refman/5.7/en/keywords.html

PHP -> mySQL Connection Errors

I'm having a connection errors trying to connect with PHP to mySQL. I'm using a video instructable, because I've been having several of these connections before, but it is outdated and I'm trying to use mysqli instead of mysql commands, but I'm winding up with nothing posting on my pages, I think because of this.
Can you tell me if this would be the correct translation of it?
function user_exists($username) {
$username = sanitize($username);
$query = mysqli_query("SELECT COUNT('user_id') FROM 'users' WHERE 'username' = '$username'");
Sanitize is defined on another page (and included) as:
<?php
function sanitize ($data) {
return mysqli_real_escape_string($data);
}
?>
Please help!
First you need connect to MySQL..is your function have that ? It is incomplete..on the screen..
example..
$link = mysqli_connect("localhost","root","","database") or die("Error " . mysqli_error($link));
$query = "SELECT `release_year` FROM film LIMIT 5" or die("Error in the consult.." . mysqli_error($link));
//execute the query.
$result = $link->query($query);

Im not getting any returned value from this... why?

Yes it connects to the database, everything else works fine. I cant seem to pull the pass from the db its showing no returned echo
<?php
$username="test";
include("db.php");
$con=mysql_connect($server, $db_user, $db_pwd) //connect to the database server
or die ("Could not connect to mysql because ".mysql_error());
mysql_select_db($db_name) //select the database
or die ("Could not select to mysql because ".mysql_error());
$query="select password from ".$table_name." where username='$username'";
$result=mysql_query($query,$con) or die('error');
while ($row = mysql_fetch_assoc($result));
$un_pass_s1=$row['password'];
echo $un_pass_s1;
?>
while ($row = mysql_fetch_assoc($result)); loops until $row is false. The loop body is a single empty statement, ;. You need to put your code which accesses $row inside the loop, not after it.
$sql=mysql_query("select password from ".$table_name." where username='$username'");
while($row=mysql_fetch_array($sql))
{
$un_pass_s1=$row['password'];
}
echo "value=".$un_pass_s1;

PHP MySQL DB query error

I am trying to get the top 10 items from a table:
<?php
include DBConnect.php;
$dbname = 'Telejoke';
mysql_select_db($dbname);
$query = "SELECT * FROM jokes LIMIT 10";
$data = mysql_query($query) or die('Error, insert query failed');
mysql_close($conn);
$info = mysql_fetch_array( $data );
?>
The PHP script keeps executing the die part saying that my query insert failed.
UPDATE:
The error is No connection could be made because the target machine actively refused it.
UPDATE 2:
I think the user I connect to DB is not authorized to use the SELECT command. This would cause the preceding error?
In db connection.php you must use a user name with password who is allowed to do operations on db.

Categories