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
Related
I am trying to get some data out of a SQL Database and show them in a dropdown menu. Show the data from the DB is not a problem, but there is a problem with the dropdown menu.
My code so far:
<?php
include 'config.php';
$sql = "SELECT car FROM [dbo].[car_available]";
$result = sqlsrv_query($conn,$sql) or die("Couldn't execut query");
while ($data=sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){
echo '<option value="'.$data['car'].'">';
echo $data['car'];
echo "</option>";
}
?>
The result is the "Couldn't execut query" message and I dont know why.
The config.php should be fine, as said a normal read out works.
I am beginner and first time works with database i have problem for executing two query.Please show me some solution for it.
My code is as follow:-
if(isset($_POST["Submit"]))
{
// echo "value is .".$a;
echo $_POST["gr_num"];
echo $_POST["school_code"];
$sqlstr="select studentname from gr_master where grid='".$_POST["gr_num"]."' and schoolcode='".$_POST["school_code"]."'";
$sqlstr1="select schoolname from school_master where schoolcode='".$_POST["school_code"]."'";
$result=mysql_query($sqlstr);
$result1=mysql_query($sqlstr1);
$row=mysql_fetch_array($result);
$row1=mysql_fetch_array($result1);
echo $row["studentname"];
$studentname_var=$row['studentname'].'"';
echo $studentname_var;
}
Here this $row1=mysql_fetch_array($result1); generates error so how to execute two query here without any function like mysqli_multi_query().
You need to debug your code using the basic die() and print_r() functions.
See where exactly your query is stuck:
$result = mysql_query($sqlstr) or die("Query 1 Error: ".mysql_error());
$row = mysql_fetch_array($result);
$result1 = mysql_query($sqlstr1) or die("Query 2 Error: ".mysql_error());
$row1 = mysql_fetch_array($result1);
echo "<pre">;
print_r($row);
print_r($row1);
Let me know what's being printed.
I will suggest you to use PDO. mysql is removed from PHP 7.
But still you want to use it, it's up to you!
And you are saying that it's an error. It's not an error. It's showing you a notice. Queries are running fine!
You can stop notices from your php.ini file or php code:
ini_set('display_errors',0);
error_reporting(E_ALL);
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";
}
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.
I'm having a problem trying to truncate the 'requestID' field from my requests table.
This is my code.
<?php
include 'mysql_connect.php';
USE fypmysqldb;
TRUNCATE TABLE requestID;
echo "Request ID table has been truncated";
?>
I'm using server side scripting so no idea what error is coming back.
Anyone got an idea?
You aren't executing queries, you're just putting SQL code inside PHP which is invalid. This assumes you are using the mysql_*() api (which I kind of suspect after viewing one of your earlier questions), but can be adjusted if you are using MySQLi or PDO.
// Assuming a successful connection was made in this inclusion:
include 'mysql_connect.php';
// Select the database
mysql_select_db('fypmysqldb');
// Execute the query.
$result = mysql_query('TRUNCATE TABLE requestID');
if ($result) {
echo "Request ID table has been truncated";
}
else echo "Something went wrong: " . mysql_error();
Take a look at the function mysql_query which performs the query execution. The code to execute a query should look something like this.
$link = mysql_connect('host', 'username', 'password') or die(mysql_error());
mysql_select_db("fypmysqldb", $link) or die(mysql_error());
mysql_query("TRUNCATE TABLE requestID", $link) or die(mysql_error());
mysql_close($link);