我知道这个问题早已被问过很多次,但其他答案都没有对我有用。我在这方面遇到了麻烦:
$row = $conn->query("SELECT * FROM urls WHERE id = '$id'");
我遵循了一个教程,所以我不知道是否还有其他信息我应该提供
编辑:
见下全文:
<?php
function idExists($id){
include $_SERVER['DOCUMENT_ROOT'] . '/short/includes/init.php';
$row = $conn->query("SELECT * FROM urls WHERE id = '.$id'");
if($row -> num_rows > 0){
return true;
} else {
return false;
}
}
function urlHasBeenShortened($url){
include $_SERVER['DOCUMENT_ROOT'] . '/short/includes/init.php';
$row = $conn->query("SELECT * FROM urls WHERE link_to_page = '$url'");
if($row->num_rows > 0){
return true;
} else {
return false;
}
}
function getURLID($url){
include $_SERVER['DOCUMENT_ROOT'] . '/short/includes/init.php';
$row = $conn->query("SELECT id FROM urls WHERE link_to_page = '$url'");
return $row->fetch_assoc()['id'];
}
function insertID($id, $url){
include $_SERVER['DOCUMENT_ROOT'] . '/short/includes/init.php';
$conn->query("INSERT INTO urls (id, link_to_page) VALUES ('$id', '$url')");
if(strlen($conn->error) == 0){
return true;
}
}
function getUrlLocation($id){
include $_SERVER['DOCUMENT_ROOT'] . '/short/includes/init.php';
$row = $conn->query("SELECT link_to_page FROM urls WHERE id = '$id'");
return $row->fetch_assoc()['link_to_page'];
}
?>
初始代码
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
行上的错误:7,18
答案 0 :(得分:2)
你忘记了数据库名称:
$conn = new mysqli($servername, $username, $password);
// Create connection like this :
$conn = new mysqli($servername, $username, $password, $dbname);
更改
$row = $conn->query("SELECT * FROM urls WHERE id = '$id'");
到
$row = $conn->query("SELECT * FROM urls WHERE id = ".$id);
也改变了:
if($row -> num_rows > 0)
到
if($row->num_rows > 0)