I have a database where I can make queries to all tables without problems except for the 'employees' table. I tried making this basic query in php:
<?php
error_reporting(0);
require "init.php";
$sql = "SELECT * FROM empleados;";
$result = mysqli_query($con, $sql);
$response = array();
while($row = mysqli_fetch_array($result)){
$response[]=$row;
}
echo json_encode($response);
?>
... and I do not get any results. However when I run this query for other tables, it works well, what can it be?
This same query works fine from phpmyadmin
NEWS: if you use SELECT DNI FROM empleados it works, if I use SELECT * FROM empleados it doesn't.... (DNI is the key, is it possible that I can only access the primary key?)
Be careful with special characters such as Spanish accents, if you remove these the query works correctly
It seems you are not connecting to database.
Try this.
$con=mysqli_connect("localhost","my_user","my_password","my_db");
$result = mysqli_query($con, $sql);
Do not forget to change the credentials.
Related
I'm busy with a school project where I need to register users. I created the database and added the tables and can add users. What I just can't get right is to display the next available user id in the table.
I'm using php to retrieve the highest value but when I use echo the variable won't show. There is no error, there is no output at all, just the rest of the page.
Here is the code:
<?php
$db = mysqli_connect('localhost', 'root', '', 'design');
$query = "SELECT MAX(userid) AS userid FROM users" or
die(mysql_error());
$highest_id = mysqli_query($db, $query);
echo $highest_id;
?>
The code successfully connects to the database, the column is called userid, it contains int values and there are other columns as well.
All other code in the script runs perfectly, it's just this part that I can't get to work.
I have spent the last two days reading and searching for answers and I am at my wits end. Any help would be appreciated.
Thank you.
could be your table is User and not Userid
$query = "SELECT MAX(userid) AS userid FROM users"
Anyway for fetching you should use eg:
$result = mysqli_query($db, $query);
$row=mysqli_fetch_array($result,MYSQLI_NUM);
echo $row[0];
The mysqli_query returns a general object that contains the results array. You have to use the mysqli_fetch_row.
<?php
$db = mysqli_connect('localhost', 'root', '', 'design');
$query = "SELECT MAX(userid) AS userid FROM userid" or die(mysql_error());
$highest_id_query = mysqli_query($db, $query);
var_dump($highest_id_query); // so you could check the object attributes
//loop results from query
while($row=mysqli_fetch_row($highest_id_query)){
$highest_id = $row['userid'];
echo $highest_id;
}
?>
You could also use the sql statement: SELECT COUNT(*) FROM userid
Be sure to name your tables correctly! SELECT COUNT(*) FROM users
I'm developing an app for android that uses a DB on a server.
I wrote some script php to create new rows in some tables and get all elements from a table (using JSON to exchange data between android and mysql).
Now I have a problem:
i need to select an id from a table and then use this to insert a row in anothere table that has this foreign key.
Well, when I try to select my id, i don't know why, but look like it doesn't work.
Here a simple example how I select this id:
//connect to DB...
$result = mysql_query (*SELECT id FROM 'table' WHERE name = $name );
$row = mysql_fetch_assoc($result);
$id = $row['id'];
When i use this to select an id, and put it in another query (always on the same connectio) nothing is stored.
if I force the value manually, and so in the same second query I put a number of a preesisting id, the insert works, so the problem is in this piece of code.
Hope someone could help me.
Thank you!
The code that you have put on the question, contains syntax errors.
- Remove * from the start of query
- put the query inside " "
- remove single quote ('table') from table name
Here is the modified code:
//connect to DB...
$result = mysql_query ("SELECT id FROM table WHERE name = $name" );
$row = mysql_fetch_assoc($result);
$id = $row['id'];
Also you should escape the parameter $name in query. And you should use mysqli or PDO instead of mysql extension.
try this:
$result = mysql_query (*SELECT id FROM 'table' WHERE name = $name );
$row = mysql_fetch_assoc($result);
while($row > 0){
$id = $row['id'];
}
session_start();
$user=$_SESSION['username'];
$con=mysqli_connect("$host","$username","$password","$db_name");
$sql1="SELECT UserID FROM User WHERE username='$user'";
$query=mysqli_query($con,$sql1);
echo $query;
Im not sure what's the problem with this, I've tried searching around here and tried different ways of writing the syntax and checking for errors in parameter but still no output for the command. What am i missing here?
UPDATE
$sql1="SELECT UserID FROM User WHERE username='$user'";
$query=mysqli_query($sql1,$con);
$row=mysqli_fetch_array($query);
$uid=$row[0];
echo $uid;
As answered by the others, I tried doing this. There is still no output or an output of 0 was given. Where if I tried the query in sql, it shows the right one.
UPDATE 2
It tried the var_dump() command and it returns NULL. Does that mean it can't read the database?
mysqli_query gives no output. A successfully query returns a resource. You have to handle the resource with mysqli_fetch_object, mysqli_fetch_assoc or mysqli_fetch_array.
Also you gave $con parameter before $sql1 which is wrong. You have to put the connection first, then the query variable.
Example:
<?php
session_start();
$user = $_SESSION['username'];
$con = mysqli_connect("$host", "$username", "$password", "$db_name");
$sql1 = "SELECT UserID FROM User WHERE username='$user'";
$query = mysqli_query($con, $sql1);
$fetch = mysqli_fetch_assoc($query);
var_dump($fetch);
?>
$query will be a "Resource". You can use for example $result = mysqli_fetch_array($query); and then echo $result[0];May there's another way, but that's a simple one!
I was able to do some workaround with my problem using mysql syntax. Still not able to determine the problem with mysqli but able to finished my task. Thanks for answering guys.
I have php script like this
$query = "select * where userid = 'agusza' ";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
echo $result;
}
when I execute, the result like this
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 'where userid = 'agusza'' at line 1
But when I run that sql in sqlserver, it running well
Anybody has solution ?
$query = "select * from table_name where userid = 'agusza' ";
See the corrections I have made. You haven't used the right syntax for SELECT query
You didn't select a table using FROM. Without that, it does not know which table you are selecting data from.
You should also stop using mysql as it is deprecated. Use mysqli or PDO as they are safer.
You are also echoing the wrong variable in your while loop, try this:
while ($row = mysql_fetch_array($result) {
echo $row['column_name'];
}
$query = "select * from table where userid = 'agusza'";
Right now, you're not telling which table SQL should look in.
You should format your query like so:
select * from `TableName` where userid='agusza'
In your query below you doesnt state the database table where you should get that data using FROM
$query = "select * where userid = 'agusza' "; // instead of this
$query = "select * FROM declaredtable where userid = 'agusza' "; used this
I'm trying to pull some information from a database, and the connection is working, but for some reason it isn't recognizing my query, even though I confirmed the query in the database with SQL and had it "generate PHP code". The echo statement is coming up blank. It's a mySQL database. Thanks for your help.
$query = "SELECT `contact` FROM `contactinfo` WHERE member=\'Henry\'";
$contact = mysqli_query($db,$query);
echo $contact;
$contact contains MySQL result object you need to fetch data from this to use this in your application.
$query = "SELECT `contact` FROM `contactinfo` WHERE member = 'Henry'";
$contact = mysqli_query($db, $query);
while ($row = mysqli_fetch_row($contact)) {
echo $row[0]; // 0 to n indicates the Column(s) Selected in SELECT Query
}