Cant show data from SQL Server - Query wont work - php

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.

Related

echo doesnt work after pg_query

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.

How to get a text string from sql database to use in html code

I want to get stings form a mysql server to use as text on my webpage.
That way I can edit the text without editing the html file.
Problem is that the code I have to get the string is quite long, and I don't want to paste it everywhere on the page.
I would also like a tip on how to get just one datafield from the server, and not the whole column like I do here.
So this is what I got. And what I think is to write a function I can call from all the places I want the webpage to get a string or field from the sqlserver. But I don't know how. Can anyone help me?
<?php
$con=mysqli_connect("localhost","user..", "passwd..","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT topic FROM web_content";
$result = $con->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
echo $row["topic"]. "<br>";
}
} else
{
echo "error";
}
$con->close();
?>
Problem is that the code I have to get the string is quite long, and i
dont want to paste it everywhere on the page.
Put the code into a function, call that function wherever you need to. Then it is just a single line you have to insert.
PHP:
<?php
function connect() {
$con=mysqli_connect("localhost","user..", "passwd..","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
return $con;
}
}
function renderContent($con) {
$sql = "SELECT topic FROM web_content";
$result = $con->query($sql);
if ($con && ($result->num_rows > 0))
{
// output data of each row
while($row = $result->fetch_assoc())
{
echo $row["topic"]. "<br>";
}
} else {
echo "error";
}
}
HTML:
<?php $con = connect(); ?>
[...]
<div>
<?php renderContent($con); ?>
</div>
[...]
I would also like a tip on how to get just one datafield from the
server, and not the whole coloumn like i do here.
Not the whole column would mean not all rows, but one or some selected ones. That means you are looking for sqls ''WHERE'' clause.
SELECT topic FROM web_content WHERE <where clause>;
Where <where clause> is some clause to narrow down the result set. For example you can narrow down to topics containing some string: ... WHERE topid LIKE '%word%'; or by the IDs are a date range of the entries in your table. You should take a look into the documentation of the query syntax for an explanation: http://dev.mysql.com/doc/refman/5.0/en/select.html
Obviously all of this is just a rough sketch of what you are looking for. Lots of things need improving. Using exceptions for error handling is one thing, just to give an example...

Displaying records from a table with 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

Outputting contents of database mysqli

Hi I know this is a little general but its something I cant seem to work out by reading online.
Im trying to connnect to a database using php / mysqli using a wamp server and a database which is local host on php admin.
No matter what I try i keep getting the error Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given when i try to output the contents of the database.
the code im using is:
if (isset($_POST["submit"]))
{
$con = mysqli_connect("localhost");
if ($con == true)
{
echo "Database connection established";
}
else
{
die("Unable to connect to database");
}
$result = mysqli_query($con,"SELECT *");
while($row = mysqli_fetch_array($result))
{
echo $row['login'];
}
}
I will be good if you have a look at the standard mysqli_connect here
I will dont seem to see where you have selected any data base before attempting to dump it contents.
<?php
//set up basic connection :
$con = mysqli_connect("host","user","passw","db") or die("Error " . mysqli_error($con));
?>
Following this basic standard will also help you know where prob is.
you have to select from table . or mysqli dont know what table are you selecting from.
change this
$result = mysqli_query($con,"SELECT *");
to
$result = mysqli_query($con,"SELECT * FROM table_name ");
table_name is the name of your table
and your connection is tottally wrong.
use this
$con = mysqli_connect("hostname","username","password","database_name");
you have to learn here how to connect and use mysqli

PHP postgreSql cannot display results

Please can someone help me with this I cant find very much information on this problem.
I am connecting fine to PostgreSql database but when I look through the array and display nothing is being displayed, however the exact amount of rows are being displayed s oi know the connection/query are the right syntax, must be the variables syntax but ive tried everything I can find to make it work, any ideas?
<?php
pg_connect("host=******** port=**** dbname=****** user=***** password=********") or die("Couldn't Connect"); // Connect to the Database
$query = "SELECT * FROM phones";
$query = pg_query($query);
while($row = pg_fetch_array($query))
{
echo "Model: ".$row['Model']."<br />";
echo "OS: ".$row['OS']."<br />";
echo "Description: ".$row['Description']."<br /><br />";
}
?>
Thank you in advance for any help
Try to use pg_last_error();
<?php
$dbconn = pg_connect("dbname=publisher") or die("Could not connect");
// Query that fails
$res = pg_query($dbconn, "select * from doesnotexist");
echo pg_last_error($dbconn);
?>
http://us2.php.net/pg_last_error
Try pg_fetch_assoc instead of pg_fetch_array, it might work. It works for MySQL.

Categories