Can anyone tell me a reason a query just wont return the same data in php as the ones that it returns on phpmyadmin sql?
$query = "UPDATE `boards` SET `contestPlaces`=0, `contestPlacesFilled`=0";
$result = mysql_query($query) or die("ERROR:QUERY_FAILED timeset 8" . mysql_error());
$query = "UPDATE `playerspoints` SET `points`=0";
$result = mysql_query($query) or die("ERROR:QUERY_FAILED timeset 9" . mysql_error());
$query = "SELECT `avatarId`, `points` FROM `contestants`";
$result = mysql_query($query) or die("ERROR:QUERY_FAILED timeset 10" . mysql_error());
$qualified = array();
while($row = mysql_fetch_row($result));
{
print_r($row);
$qualified[] = $row;
} `
Result: Array ( [0] => ) SUCCESS.
I get no error, it just returns an empty result, while in phpmyadmin sql tab, it runs fine.
I'm properly connected with the db, cause I run queries before this one. I checked to see and this one is the only one that fails without an apparent reason. So What should I look in order to see what's going wrong?
The user that I get connected to the database has the following privileges:
SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, CREATE VIEW, EVENT, TRIGGER, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EXECUTE
remove the semicolon:
while($row = mysql_fetch_row($result));
^
//this is latest code in php5.7.14 and accesses the database.....but still it was not working.
//therefore, i found that the database coallition was not 'utf_general_ci' rather i kept default leading to 'latin_swedish'.
//single mistake can ruin your project.
<?php
$mysqli_hostname = "localhost";
$mysqli_user = "root";
$mysqli_password = "krunal";
$mysqli_database = "krunal";
$bd = mysqli_connect($mysqli_hostname, $mysqli_user, $mysqli_password,$mysqli_database);
if(mysqli_connect_errno()){die("database connection failed");}
?>
<?php
$sql= "SELECT * FROM `done`;";
$result=mysqli_query($bd,$sql);
if(!$result){
die("database query failed". mysqli_connect_error()." (".mysqli_connect_errno().")");}?>
<?php while($row=mysqli_fetch_row($result)){
var_dump($row); }?>
<?php mysqli_close($bd);?>
Related
<?php
//after creating connection
$dbname = 'bca2y';//database name
$sql = "SHOW TABLES FROM $dbname";
$result = mysqli_query($conn , $sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysqli_fetch_row($result)) {
$table = "{$row[0]}\n";
echo "$table<br>";
if($conn == TRUE){
echo "connection is possible<br>";
}
$sqld = "DELETE FROM $table";
$resultd = $conn->query($sqld);
if($resultd === TRUE){
echo "Data deleted succesfully ";//checking if data deleted
}
else {
echo "some error<br>";// for checking if code is not running
}
}
?>
Here I am trying to find name of table and delete data from the given table
But I think there is any syntax error in using variable as a table name.
my code has not giving any error but it still not working.
If I am understanding your intention -- to find table(s) and delete them from the database, "DELETE FROM mes" does not accomplish this goal. In SQL, "DELETE FROM" removes records from the specified database. It is more common to see DELETE FROM TableName WHERE ColumnName is 'SomeValue'; -- a deletion of some set of records. But DELETE FROM TableName is a valid SQL query -- it deletes all records from the table named TableName.
If you wish to delete all records from the table names retrieved from your first query, you would need to use a variable name in your delete statement rather than the static string mes.
If you want to remove the table (not just delete the data contained therein), use DROP TABLE.
If you want to DELETE all rows in a table, you should use TRUNCATE.
DELETE will delete row per row, which might take a lot of time because a lot of stuff is done by mysql for each delete operation. TRUNCATE will empty your table almost instantly
Here's your script adapted to achieve what you want
$dbname = 'bca2y';//database name
$sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA LIKE '".$dbname."'";
$result = mysqli_query($conn , $sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
$rows = $result->fetch_all(MYSQLI_ASSOC);
foreach($rows as $table)
{
$sql = "TRUNCATE TABLE `".$table['TABLE_NAME']."`";
$resultd = $conn->query($sql);
if($resultd === TRUE){
echo $table['TABLE_NAME']. " truncated succesfully ";//checking if data deleted
}
else {
echo "some error<br>";// for checking if code is not running
}
}
Your user should have TRUNCATE permissions of course. And THINK before executing this, it is a dangerous script if used on the wrong database.
I just create a Remote MySQL access from my other database server (remote) and I can see tables in it :
// SHOW ALL TABLES NAME
$sql = "SHOW TABLES";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_row($result)) {
echo "Table: {$row[0]} <br>" ;
}
I can also see what columns name on each tables by using this :
// SHOW COLUMNS NAME OF A TABLE
$sql = 'DESCRIBE wp_ps_product_sku';
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_row($result)) {
echo "Column: {$row[0]} <br>" ;
}
but why I can't see any rows inside each table?
$sql = 'SELECT * wp_ps_product_sku';
$result = $conn->query($sql);
print_r($result);
it always gives me an empty result. for any tables. same result. it's always empty. what did I missed here? is it possible that server just give me an access to read the database structure only, but not the data?
thank you.
On your select statement, you are missing the 'from' clause.
select * from wp_ps_product_sku;
Ive been trying to display a "bid" from the database to no success.
here is my error
Fatal error: Function name must be a string in /home/rslistc1/public_html/get-bids.php on line 7
here is my code
<?php
include('session.php');
?>
<?php
require_once('mysql_connect.php');
$query3 = "SELECT id, username, bid FROM bids WHERE username = '$login_session'";
$result3 = mysql_query($query3) OR die($mysql_error());
$num = mysql_num_rows($result3);
while ($row = mysql_fetch_array($result3, MYSQL_ASSOC)) { ?>
<?php echo''.$row['bid'].'';
}
?>
Any idea
Before we address the line 7 issue, lets check other errors. In order to request a query to a MYSQL database, we need to create a connection:
$con = mysqli_connect("ip_address","user","password","database_name");
Once we have that connection, let us check if we can actually connect to the database:
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
Appreciate that mysqli_error() function uses the connection. Now the query string:
$query3 = "SELECT id, username, bid FROM bids WHERE username = '$login_session'";
You are sending a query to look for a username called "$login_session" and it would most likely not find any match. To add strings from variables will be as follow:
$query3 = "SELECT id, username, bid FROM bids WHERE username = '" . $login_session . "'";
Now, for the error in line 7
result3 = mysql_query($con, $query3) OR die($mysql_error($con));
As you can see, both mysql function use the connection to check for errors. Try it and let me know if everything works fine.
Edit:
Terribly sorry my friend, I just forgot to put a little letter "i" on the line, also, I would like to show you my way to deal with the query result. First, the line as it should be:
$result3 = mysqli_query($con, $query3);
Notice the i after mysql. Now let us check whether we got some rows or not:
if (!$result3) {
die('Could not retrieve data: ' . mysqli_error($con));
} else {
while ($row = mysqli_fetch_array($result3)) {
//Show your results
}
}
I am using a small script to automate schema transfers between servers, but now I will need to modify it to also include data. I am not that good with PHP so I am asking for your help.
This is what I have so far (parts I got from another source, not mine):
/*********** GRAB OLD SCHEMA ***********/
$db1 = mysql_connect($DB_SRC_HOST,$DB_SRC_USER,$DB_SRC_PASS) or die(mysql_error());
mysql_select_db($DB_SRC_NAME, $db1) or die(mysql_error());
$result = mysql_query("SHOW TABLES;",$db1) or die(mysql_error());
$buf="set foreign_key_checks = 0;\n";
$constraints='';
while($row = mysql_fetch_array($result))
{
$result2 = mysql_query("SHOW CREATE TABLE ".$row[0].";",$db1) or die(mysql_error());
$res = mysql_fetch_array($result2);
if(preg_match("/[ ]*CONSTRAINT[ ]+.*\n/",$res[1],$matches))
{
$res[1] = preg_replace("/,\n[ ]*CONSTRAINT[ ]+.*\n/","\n",$res[1]);
$constraints.="ALTER TABLE ".$row[0]." ADD ".trim($matches[0]).";\n";
}
$buf.=$res[1].";\n";
}
$buf.=$constraints;
$buf.="set foreign_key_checks = 1";
/**************** CREATE NEW DB WITH OLD SCHEMA ****************/
$db2 = mysql_connect($DB_DST_HOST,$DB_DST_USER,$DB_DST_PASS) or die(mysql_error());
$sql = 'CREATE DATABASE '.$DB_DST_NAME;
if(!mysql_query($sql, $db2)) die(mysql_error());
mysql_select_db($DB_DST_NAME, $db2) or die(mysql_error());
$queries = explode(';',$buf);
foreach($queries as $query)
if(!mysql_query($query, $db2)) die(mysql_error());
How can I modify the existing code to include data as well?
Thanks!
If you need to transfer both the data /and/ the schema, why not do a mysqldump, and just read the whole thing back in? What does this not to, that the script is supposed to be doing?
After going though a lot of links and tutorials, I managed to come up with something that simply works! Here is the code:
$connectDST = mysql_connect($DB_DST_HOST, $DB_DST_USER, $DB_DST_PASS);
mysql_select_db($DB_DST_NAME, $connectDST);
set_time_limit(0);
$connectSRC = mysql_connect($DB_SRC_HOST, $DB_SRC_USER, $DB_SRC_PASS);
mysql_select_db($DB_SRC_NAME, $connectSRC);
$tables = mysql_query("SHOW TABLES FROM $DB_SRC_NAME");
while ($line = mysql_fetch_row($tables)) {
$tab = $line[0];
mysql_query("DROP TABLE IF EXISTS $DB_DST_NAME.$tab") or die('Couldn\'t drop table:'.mysql_error());
mysql_query("CREATE TABLE $DB_DST_NAME.$tab LIKE $DB_SRC_NAME.$tab") or die(mysql_error()) or die('Couldn\'t create table:'.mysql_error());
mysql_query("INSERT INTO $DB_DST_NAME.$tab SELECT * FROM $DB_SRC_NAME.$tab") or die('Couldn\'t insert data:'.mysql_error());
echo "Table: <b>" . $line[0] . " </b>Done<br>";
}
I tried to avoid exec, since some hosts do not allow it...
Hello guys i am trying to show all the users pokemon were the table belongsto = there username here is my code
i have a connect on top of this has well
// Get all the data from the "example" table
$result = "SELECT * FROM user_pokemon WHERE
belongsto='".$_SESSION['username']."'
AND (slot='0')'";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo $row['pokemon'];
echo $row['id'];
}
i have print red the username and there username is in the username session .
i think i mite be missing a ' or something i add or mysql at the end of the query but then the pages dies with no error
You are not running the query and have an error in it. And you're not escaping strings going into query.
A proper version of the code would be
// escape a string going to query.
$username = mysql_real_escape_string($_SESSION['username']);
// create a query
$sql = "SELECT * FROM user_pokemon WHERE belongsto='$username' AND slot=0";
// run a query and output possible error for debugging purposes.
$res = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
// keep getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo $row['pokemon'];
echo $row['id'];
}
It appears to me that the final query would be:
SELECT * FROM user_pokemon WHERE belongsto='NAME' AND (slot='0')'
where NAME is the name you pass in. If that is the case, there is an extra single quote at the end. I presume you are getting a SQL error?