I am trying to display a table in php. I have established a valid connection. I get the error:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /Applications/XAMPP/xamppfiles/htdocs/project.php on line 17
The page's code:
<html>
<head>
<title>PHP Site Michael Mazur</title>
</head>
<body>
<?php
//connect to DB
$con=mysql_connect("localhost","mike","mike");
$db_found = mysql_select_db("my_guitar_shop2");
$result = mysql_query("SELECT firstName,lastName FROM customers");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['firstName'] . "</td>";
echo "<td>" . $row['lastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</body>
</html>
The rest of your while loop could look like this
while($row = mysql_fetch_array($result)){
print "<tr><td>".$row['Firstname']."</td><td>".$row['Lastname']."</td></tr>";
}
print "</table>";
Try putting these lines on a single line as i have done above.
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
like
echo "<table border='1'><tr><th>Firstname</th><th>Lastname</th></tr>";
Other useful options here.
http://php.net/manual/en/function.mysql-fetch-array.php
http://php.net/manual/en/control-structures.foreach.php
Given that part of the page's code is missing, this is only a guess. But it looks like part of your problem is that you've double-selected the database (a no-no).
Also, the while statement looks slightly suspect (no opening brace to verify context).
Additionally, if you are going to pass $con to the database selector, you should also pass it to the mysql_query calls (good practice for readability).
Related
I'm trying to output the data from a specific table within my Oracle SQL Developer database using a PHP form. I had some experience with this concept in the past using MySQL but I'm struggling to understand the differences between integrating it using Oracle specific syntax now.
My code is ideally laid out but when I run the code I get an error regarding the OCI_FETCH_ARRAY parameters, which I'm unsure on how to solve.
My Code
<?php
include("../connection.php");
error_reporting(E_ALL);
$sql = 'SELECT * FROM ALGORITHM';
$stid = oci_parse($conn, $sql);
echo "<table border='1'>";
echo "<tr> <th>ID</th> <th>Name</th> <th>Description<th> </tr>";
while($row= oci_fetch_array($stid, $sql))
{
echo "<tr>";
echo "<td>" . $row ['Algorithm_ID'] . "</td>";
echo "<td>" . $row ['Algorithm_Name'] . "</td>";
echo "<td>" . $row ['Algorithm_Role'] . "</td>";
echo "</tr>";
}
echo "</table>";
oci_free_statement($stid);
oci_close($conn);
?>
The Error I keep getting
Warning: oci_fetch_array() expects parameter 2 to be integer, string
given in
/studenthome.hallam.shu.ac.uk/STUDENTHOME8/1/b5040831/public_html/ADM/bin/php/entities/database.php
on line 12
I get that it's asking for an integer, but why? In the MySQL concept you simply outline the connection string it was never an issue.
Can anyone help ?
Calling oci_fetch_array() ( from http://php.net/manual/en/function.oci-fetch-array.php) should be called like
while($row= oci_fetch_array($stid))
The second parameter is the mode - i.e. OCI_ASSOC.
Update: when using oci_parse(), this doesn't actually execute the parsed statement, you need to do...
oci_execute($stid);
So your code would be something like...
$sql = 'SELECT * FROM ALGORITHM';
$stid = oci_parse($conn, $sql);
oci_execute($stid);
echo "<table border='1'>";
echo "<tr> <th>ID</th> <th>Name</th> <th>Description<th> </tr>";
while($row= oci_fetch_array($stid, OCI_ASSOC))
(Look at http://php.net/manual/en/function.oci-parse.php for code examples)
Hi so im trying to put all of the useres in a database, table into a html table this is what i have:
<table class="table table-striped">
<thead>
<tr>
<th>UUID</th>
<th>Full Name</th>
<th>Email</th>
<th>Access Key</th>
<th>Phone Number</th>
<th>Activated</th>
<th>Role</th>
</tr>
</thead>
<tbody>
<?php
include_once('inc/conf/databaseConnect.php');
$query = mysql_query("SELECT * FROM list_users ORDER by id");
while($row = mysql_fetch_array($query)){
echo "<tr>";
echo "<td>".$row['uuid']."</td>";
echo "<td>".$row['firstname'].$rowtwo['lastname']."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['security_key']."</td>";
echo "<td>".$row['phone_no']."</td>";
echo "<td>".$row['activated']."</td>";
echo "<td>".$row['role']."</td>";
echo "</tr>";
}
?>
</tbody>
</table>
This dosnt return anything, or any errors. It connects to the database correctly ive checked that its just not retrieving the users.
Image of database structure
databaseConnect.php:
<?php
//Create Connnection
$sqlLink = mysqli_connect('localhost', 'root', 'classified', 'user_details');
//If Error Connecting
if(!$sqlLink) {
die('<center><br><h3>Error connecting to servers Database.');
}
?>
After seeing your edit because you did not originally show us which connection method you were using; the almighty answer here (least the most important one) is that you can't mix different MySQL APIs.
Can I mix MySQL APIs in PHP?
You need to use the same one from connection to query.
Plus that $rowtwo should be $row as I stated in comments along with my asking about what was inside your databaseConnect.php file.
Get to work with prepared statements also to help protect against an SQL injection.
https://en.wikipedia.org/wiki/Prepared_statement
Thanks. I have fixed the issue i updated to using mysqli's methods
<?php
include_once('inc/conf/databaseConnect.php');
$query = $sqlLink->query("SELECT * FROM list_users ORDER by id");
while($row = $query->fetch_array()){
echo "<tr>";
echo "<td>".$row['uuid']."</td>";
echo "<td>".$row['firstname'].$row['lastname']."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['security_key']."</td>";
echo "<td>".$row['phone_no']."</td>";
echo "<td>".$row['activated']."</td>";
echo "<td>".$row['role']."</td>";
echo "</tr>";
}
?>
At least check for errors:
if ($query = mysql_query("SELECT * FROM list_users ORDER by id");) {
...
} else {
echo '<b>MySQL error:</b><br>' . mysql_error() . '<br />';
}
You must use mysql_fetch_assoc() instead of mysql_fetch_array or fetch like this mysql_fetch_array($result, MYSQL_ASSOC).
I have a site where members can mark other members as 'a favourite'. User are able to search the members table in various ways and I want to show from any of the results that are returned whether or not the users returned are favourites of the current user.
This is some very simplified code I have been using to try and get this query to work but I just can't figure it out. Whenever I add 'GROUP BY' to avoid duplicate results from my LEFT JOIN the 'if' statement does not work. The 'if' statment does work however, if I omit the 'GROUP BY' but I get all rows from members table and the favourites table. Thanks.
$result = mysqli_query($db_conx, "SELECT members.*, user_favourites.* FROM members LEFT JOIN user_favourites ON members.id = user_favourites.fav_id GROUP BY members.id");
echo "<table border=''>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>A favourite of User</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
if ($visitor == $userid ){
$msgs = "x";
}
else { $msgs = "0";
}
echo "<td>". $msgs. "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I suggest to you, tu use 2 query's insted one. Simple query is a fast querys. If your site scale up, your query will be slow query and it cause performance problems.
Idea:
For one hand: SELECT FROM members, get all and put it inside array, key=member.id
$members[$row['member_id']]=$row;
On the other hand: SELECT * FROM user_favourites, and put it inside the previous array, refereced by the key fav_id use distinc if you have duplicates.
$members[$row['fav_id']]['favourites']=$row;
Perfect now you have all you need, an array with all information, iterate it.
JilianJ something like this:
<?
//Prepare
$all_users=array()
$query_users='SELECT * from user';
$query_favourites='SELECT * FROM user_favourites';
//Now I find all members information
$users=mysqli_query($db_conx,$query_users);
while($user = mysqli_fetch_array($users)) {
$all_users[$user['id']]=$user;
}
//Now I add favourite information to the users information
$favourites=mysqli_query($db_conx,$query_favourites);
while($favourite = mysqli_fetch_array($favourites)) {
$all_users[$favourite['fav_id']]['fovourite'][]=$favourite['fav_id'];
}
?>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>A favourite of User</th>
</tr>
<? foreach ($information as $key=>$item) {?>
<tr>
<td><?=$item['firstname'];?></td>
<td><?=$row['lname'];?></td>
<td><?=$row['email'];?></td>
<td>
<? foreach($item['favourites'] as $key2=>$item2) { ?>
<p><?=$all_members[$item2]['name];?></p>
<? } ?>
</td>
</tr>
<? } ?>
</table>
Good luck
Basicaly having issues setting up a webpage which will taken in a student key entered by the user. This will then parse the student key to another file which will run it against a mysql backend to see what records this student already has. But can not get it working for the life of me please help I'm still a newb at this.
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("support_log", $con);
$result= mysql_query("SELECT student.first_name, student.surname, student.year_group, student.STKEY, student_log.issue
FROM `student` JOIN `student_log`
WHERE student.STKEY like '$_POST[stkey]'");
$result2 = mysql_query($result) or die("Error: " . mysql_error());
if(mysql_num_rows($result2) == 0){
echo("no records found");
} ELSE {
echo "<table border='1'>
<tr>
<th>First name</th>
<th>Surname</th>
<th>Year Group</th>
<th>Student Key</th>
<th>Issue</th>
</tr>";
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['First_Name'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['year_group'] . "</td>";
echo "<td>" . $row['stkey'] . "</td>";
echo "<td>" . $row['issue'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
mysql_close($con);
?>
After changing my where statement to:
WHERE student.STKEY like '$_POST[stkey]'");
I am no longer reciving errors from PHP but now recieving the error Query was empty which is part of my code to detect if there is no results. Though I have tested that query in phpmyadmin and it spits out results. From looking at the code does anyone have any solutions? I have also checked the parse by running an echo on the post command to ensure the data being entered was correct.
Edit: Got rid of the whole result2 check now throwing a:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\stkey_submit.php on line 24
Try $_POST['stkey'] instead of $_POST[stkey]
EDIT : if you use it in a query, it would be preferable to do :
$stkey = mysql_real_escape_string($_POST['stkey']);
$sql = "SELECT ....... like '$stkey'";
mysql_query($sql);
$result= mysql_query("SELECT student.first_name, student.surname, student.year_group, student.STKEY, student_log.issue
FROM `student` JOIN `student_log`
WHERE student.STKEY like " . $_POST["stkey"]);
How about storing the value of stkey on a variable before including it on the query?
$stkey = $_POST['stkey'];
$result= mysql_query("SELECT student.first_name, student.surname,
student.year_group, student.STKEY, student_log.issue
FROM `student` JOIN `student_log`
WHERE student.STKEY LIKE '%$stkey%'");
You might also want to use MySqli or PDO instead of the MySql database API. Take a look at this post from Nettuts: http://net.tutsplus.com/tutorials/php/php-database-access-are-you-doing-it-correctly/
$sql1=mysql_query("SELECT * FROM Persons", $con);
echo "<table border="3">
<tr>
<th>Name</th>
<th>Age</th>
</tr>";
while($info=mysql_fetch_array($sql1))
{
echo "<tr>";
echo "<td>" . $info['fname'] . "</td>";
echo "<td>" . $info['age'] . "</td>";
echo "</tr>";
}
echo "</table>";
this code is a part of a code which is trying to retrieve data from the table"Persons",
there is some error in this part of the code..
You have double quotes in the quoted html. Try using single quotes in stead, i.e.
echo "<table border='3'> <--- here
<tr>
<th>Name</th>
<th>Age</th>
</tr>";
Your code looks ok, except for the unescaped double quotes:
It should be:
echo "<table border=\"3\"> ... ";
or
echo '<table border="3"> ... ';
Make sure it is enclosed in <?php and ?>.
Also make sure your db columns names of fname and age really exist....
Make sure you're getting what you think back from the DB, by using print_r($info) or var_dump($info).
Finally, your connection $con could be broken / not working. You can check that by using:
if ( ! $con ) {
die('Could not connect: ' . mysql_error());
}
$sql1 = mysql_query("SELECT * FROM Persons", $con);
...
Beside double quotes in second line.
mysql_fetch_array return array indexed by integer if you want asoc array indexed by fields use mysql_fetch_assoc
while ($info=mysql_fetch_assoc($sql1)) {
...
}