The below code re-creates the issue I can't get around. I just can't seem to figure out where the problem lies - in the code? MySQL settings? or somewhere else? Any pointers in the right direction will be appreciated.
<html>
<head></head>
<body>
<?php
$db_name = "UserDB";
$open = mysql_connect("localhost", "root", "");
if($open)
echo "1. Successfully connected to MySQL";
echo "</br>";
$db = mysql_select_db($db_name, $open);
if($db)
echo "2. Successfully selected {$db_name} database";
echo "</br>";
$sql = "SHOW TABLES FROM `{$db_name}`";
$result = mysql_query($sql);
$print = mysql_num_rows($result);
if($result)
echo "3. {$print} tables found in {$db_name}";
?>
</body>
</html>
Here's my output:
1. Successfully connected to MySQL
2. Successfully selected UserDB database
3. 0 tables found in UserDB
The problem lies in line 3 of the output. It says "0" tables, which is incorrect. I have created "3" InnoDB tables in the selected DB. If I copy/paste and run the same SHOW TABLES query in phpmyadmin, it runs perfectly.
Any idea what is going on here??
Try using the wrapper function mysql_list_tables. I found it impossible once to use the SHOW TABLES due to some weird permissions definition, though I could use mysql_list_tables.
Related
I'm currently trying to retrieve information from my database and display this data with a php file but it doesnt work as intended. Here is my code:
<?php
require("dbconnect.php");
if(!$db){
echo "Error: Unable to open database";
} else {
echo "Opened database successfully";
}
$result = pq_query($db, 'SELECT * FROM example');
if (!$result) {
echo "Error: Cant access table";
exit;
}
else {
echo "Everything works fine";
}
pg_close($db);
?>
Note: dbconnect.php opens the connection to the database with pg_connect() and saves this to $db.
I expected that it displays Opened database successfully Error: Cant access table because I havent created a table example yet. But I only get Opened database successfully. So I added echo "Test"; before pg_close($db); and expected that it displays Opened database successfully Test but no, it only shows Opened database successfully.
I then tried to create a new table with php so I added
pg_query($db, 'DROP TABLE IF EXISTS example');
pg_query($db, 'CREATE TABLE example (col char(1))');
before $result = pq_query($db, 'SELECT * FROM example');. I connected with ssh to the server after this and checked with psql if the table exists and it did, so the connection should work properly. But it still only shows me Opened database successfully and I expected Opened database successfully Everything works fine. I really dont know why every echo after $result = pq_query($db, 'SELECT * FROM example'); doesnt work. Can someone explain to me whats wrong?
Change pq_query for pg_query.
Note you have a Q instead of a G.
I'm trying to pass data from a MySQL database to a HTML combobox and i'm using php to do that
<form method="POST">
<select class="js_inline_input">
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "js_milhoes";
$conn = new mysqli($servername, $username, $password, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
echo "<option> Connected successfully </option>";
}
$sql = "SELECT * FROM js_country";
$result = $conn->query($sql);
while($row=mysqli_fetch_assoc($result)){
//My Personal echo
echo "<option value='".$row['countryCode']."'>".$row['countryName']."</option>";
//Echo i saw on this site
echo "<option value='{$row->countryCode}'>{$row->countryName}</option>";
}
$conn->close();
?>
</select>
</form>
It should list all the countries in the combobox but what displays is:
- From first echo: ".$row['countryName']."
- From second echo: {$row->countryCode}
I already check the connection, and before i add a if that says that the query is not empty
The reason why your second <option> isn't showing is because you are using a "fetch object" syntax $row->column.
http://php.net/manual/en/mysqli-result.fetch-object.php
You need to use one or the other; not both.
Either you use
while($row=mysqli_fetch_assoc($result)){
echo "<option value='".$row['countryCode']."'>".$row['countryName']."</option>";
}
or (and escaping with double-quotes):
while($row=mysqli_fetch_object($result)){
echo "<option value=\"{$row->countryCode}\">{$row->countryCode}</option>";
}
You also need to note that column names are case-sensitive when iterating over those like that.
Meaning that, countryCode and countrycode are two different animals here, as is countryName and countryname; should it be the case.
Check for errors on your query.
Footnotes:
You state: "but what displays is: - From first echo: ".$row['countryName']." - From second echo: {$row->countryCode}"
I don't understand what you mean by that. I've tested your code and it works fine (besides the "fetch object" syntax issue). If what you mean by that is you are seeing "code" rather than being parsed, then you are either not using a webserver, or that you are accessing it as file:///file.php rather than http://localhost/file.php or as .html should this be the case as the file extension is unknown.
That is my conclusion for this question.
Ok, this was a stupid problem, when i changed from wamp 2.5 to wamp 3 i forget to delete the wwamp paste and i had 2, so i has using the wrong fill and because that one wasn't in a server with apache the browser comments the php
Sorry for the newbie question, but I am working on my first PHP script and I can't seem to make it work. I just want to display the records from a single MySQL table. I have been trying to do this for ages and it is not displaying anything except the first two echo statements, before it is supposed to pull out the data.
What am I doing wrong?
<?php
mysql_connect("localhost", "me", "mypass") or die(mysql_error());
echo "Connection to the server was successful!<br/>";
mysql_select_db("test") or die(mysql_error());
echo "Database was selected!<br/>";
$result = mysql_query("SELECT * FROM Customer");
while($row = mysql_fetch_assoc($result)){
echo "ID: ".$row['customer_id'].", Name:".$row['customer_name']
."<br/>";
}
?>
echo mysql_num_rows($result);
to know the number of rows returned by your query.
This error is because the table or the database you are trying to connect doesnt exits.
As #barmar suggests table names are case sensitive..
Please make sure that you are using the correct database and table ..THanx
I was uploading data from my android application to a PHP file then inserting it to Mysql database, the problem i am having that i buy a new hosting plan and when i configured everything in the new hosting, i try to upload data from the application it shows only blank fields in the table, i am sure that there's no problem in the PHP or the android code, cause it was working fine and great with the old hosting.. i tried to change the encoding but same issue.
Here's the PHP file:
<?php
$con = mysql_connect("HOST","USER","PASS");
if (!$con)
{
die('Could not Connect:'. mysql_error());
}
mysql_select_db("TABLE",$con);
mysql_query ("INSERT INTO table (rep_desc,dateT) VALUES ('".$_REQUEST['report_Desc']."','".$_REQUEST['Date_Time']."')");
mysql_close($con);
?>
Thanks in advance
If there is any auto_increment column in table. Better check if its auto_increment flag not got uncheck.
Please also check length of database fields and data that you actually trying to insert.
Name of all request variables, columns, tables should be in proper case if it is a UNIX system.
get all the tables by something like this query ...
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_row($result)) {
echo "Table: {$row[0]}\n";
}
I am running a script to update a MySQL table, and I have been echoing the update statement to help troubleshoot why the update statement was not working. When I copy and paste the echoed query for the mysql update into the SQL menu in phpmyadmin, it works perfectly, but when I run the script the update query does not execute. I know I have successfully connected to the database. Does anyone know why an echoed version of the update statement copy and pasted into the sql tab of phpmyadmin would execute successfully, but not the actual query that is supposed to be executed the line before the echoed statement? Here is my php:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Untitled 1</title>
</head>
<body>
<?php
$user = 'tunemashercom';
$pass = 'password';
$host = 'tunemashercom.ipagemysql.com';
$db_name = 'user_library';
# $db = mysqli_connect($host, $user, $pass, $db_name);
if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database.';
exit;
}
$oldsongs = $_POST['oldsongs'];
$newsongs = $_POST['newsongs'];
$count = count($newsongs);
$count2 = count($oldsongs);
echo "COUNT 1: ".$count." - COUNT 2: ".$count2."<br />";
for($i=0;$i<=$count;$i++){
$newtitle = $newsongs[$i]["song"];
echo $newtitle." - ";
if($newtitle == ""){
echo "NO NEW TAGS<br />";
}
else{
$title = $oldsongs[$i]["title"];
echo $title."<br />";
$artist = $oldsongs[$i]["artist"];
$album = $oldsongs[$i]["album"];
$newartist = $newsongs[$i]["artist"];
$newalbum = $newsongs[$i]["album"];
$mbid = $newsongs[$i]["mbid"];
$insert = "UPDATE collection SET song='".$newtitle."', artist='".$newartist."', album='".$newalbum."', gille_id='".$mbid."' WHERE song='".$title."' AND artist='".$artist."' AND album='".$album."'";
$results = $db->query($insert);
echo $insert."<br />";
}
}
mysqli_close($db);
?>
</body>
</html>
Without seeing the error it's difficult to know what the problem it, however, from my personal experience when that's happened to me on UPDATE queries it was always because I wrapped an ID with quotes.
Hopefully that helps.
The problem can be related to quotes. While typing single quotes are 'sumthng' but while executing update query, we need sumthng. I hope this works. I faced the same problem and it was solved using the quotes copied from phpmyadmi directly.
Make sure that the phpmyAdmin is the same database as the php code so "tunemashercom.ipagemysql.com"
Check to make sure that the database that you are (running the echo query) using phpmyAdmin is the same as the one what you are connecting to with your PHP code.
For example that you are connecting to phpmyAdmin from your local machine and your php code is connecting to your database at hosting server.