<?php
$dbhost='localhost:3306';
$dbuser='root';
$dbpass='';
$con=mysql_connect($dbhost,$dbuser,$dbpass);
if(!$con) {
die("Couldnotconnect:".mysql_error());
}
mysql_select_db('shop');
$sql="SELECTCD_ID,Title,Artist,Year,Company,Price,Quantity,TypeFROMcd_data";
$result=$con->query($sql); //somethingiswronghere
$numRows=$result->num_rows; //somethingiswronghere
if($result->num_rows) { //somethingiswronghere
//outputdataofeachrow
//somethingiswronghere
while($row=$result->fetch_assoc($sql)) {
echo"id:".$row["CD_ID"]."Name:".$row["Title"]."".$row["Artist"].$row["Year"].$row["Company"].$row["Price"]."Euro".$row["Quantity"].$row["Type"];
}
}
else {
echo"0results";
}
mysql_close($con);
?>
Asked
Active
Viewed 8,217 times
-4

Jens
- 67,715
- 15
- 98
- 113

hellfireworld
- 3
- 1
- 2
-
2_Warning_ This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the [MySQLi](http://php.net/manual/en/book.mysqli.php) or [PDO_MySQL](http://php.net/manual/en/ref.pdo-mysql.php) extension should be used. – AddWeb Solution Pvt Ltd Jan 19 '16 at 12:17
-
1You are using `mysql_*` for database connection and fetching records in `mysqli_*` which will not work. – Pupil Jan 19 '16 at 12:17
-
1your sql is wrong `"SELECT CD_ID,Title,Artist,Year,Company,Price,Quantity,TypeFROMcd_data";` – urfusion Jan 19 '16 at 12:18
-
What does mean the **somethingiswronghere**? – AddWeb Solution Pvt Ltd Jan 19 '16 at 12:19
-
Your question content is very poor..! – AddWeb Solution Pvt Ltd Jan 19 '16 at 12:20
-
The site doesn't let me to put the entire text as code thats why i wrote that ... i am new user in this site – hellfireworld Jan 19 '16 at 12:28
-
1please instead of criticize my question content and other stuff can you help me by giving me the solution with code? – hellfireworld Jan 19 '16 at 12:31
-
its better to help him, or mark as duplicate, instead of criticize – devpro Jan 19 '16 at 12:58
-
@hellfireworld: check the example that i have shared in answer.... – devpro Jan 19 '16 at 13:00
1 Answers
1
There are so many issues in your code:
- You want to use mysqli_* but using mysql_* functions. You can't mix both.
- Your query is not looking good.
SELECTCD_ID,Title,Artist,Year,Company,Price,Quantity,TypeFROMcd_data
I think your query should be like this:
SELECT CD_ID,Title,Artist,Year,Company,Price,Quantity,Type FROM cd_data
Here is the complete example of your code by using MYSQLi Object Oriented:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT CD_ID,Title,Artist,Year,Company,Price,Quantity,Type FROM cd_data";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//your stuff
}
}
else
{
echo "0 results";
}
$conn->close();

devpro
- 16,184
- 3
- 27
- 38
-
1Thanks a lot man you save me :)... my query spaces was edited by the site (SELECT CD_ID...) it was the same as you wrote it. But all the the other code was wrong :P . It works great! – hellfireworld Jan 19 '16 at 16:08