मैं अपने खोज बार के लिए दिशानिर्देश के रूप में इस लिंक का अनुसरण कर रहा हूं https://www.youtube.com/watch?v=Wvs6sbLN3NI&t=678s
मैं PHP और sql में एक नौसिखिया हूँ कृपया इस के साथ मेरी मदद करें। मुझे नहीं पता कि एसक्यूएल सिंटैक्स में क्या करना है। त्रुटि है, सूचना: अपरिभाषित चर: q में C:\xampp\htdocs\login-system\search.php लाइन 21 पर अग्रिम धन्यवाद।
<?php
$con = mysql_connect('localhost','root','');
$db = mysql_select_db('accounts');
?>
<!DOCTYPE html>
<html>
<head>
<title> Search Form </title>
<link rel="stylesheet" type="text/css" href="css/stylesearch.css">
</head>
<body>
<form action = "search.php" method ="GET" id="searchForm">
<input type="text" name="q" id="searchBox" placeholder="Search Here" value="" /><input type="submit" id="searchBtn" value ="GO" />
</form>
<?php
$query = mysql_query("SELECT * FROM provider WHERE provider LIKE '%$q%' OR broker LIKE '%$q%' OR deductible LIKE '%$q%' OR deductible_waiver LIKE '%$q%' OR accredited_banks LIKE '%$q%' OR can_not_offer_mortgaged_with LIKE '%$q%' OR casa_eligibility LIKE '%$q%' OR depreciation LIKE '%$q%' OR special_quotes LIKE '%$q%' OR unit_age_limit LIKE '%$q%' OR provider_information LIKE '%$q%' OR provider_branches LIKE '%$q%' OR mmx_exclusive LIKE '%$q%' OR disclaimer LIKE '%$q%' OR accredited_shops LIKE '%$q%' OR roadside_assistance_number LIKE '%$q%' OR roadside_assistance_coverage LIKE '%$q%' OR check_addressee LIKE '%$q%' OR accounts_details LIKE '%$q%' OR cancellation_charge LIKE '%$q%'");
$num_rows = mysql_num_rows($query);
while($row = mysql_fetch_array($query))
{
$id = $row['id'];
$prov = $row['provider'];
$brok = $row['broker'];
$deduc = $row['deductible'];
$deduc_waiver = $row['deductible_waiver'];
$accre_banks = $row['accredited_banks'];
$mort = $row['can_not_offer_mortgaged_with'];
$casa_elig = $row['casa_eligibility'];
$depre = $row['depreciation'];
$s_quotes = $row['special_quotes'];
$age = $row['unit_age_limit'];
$prov_info = $row['provider_information'];
$prov_branch = $row['provider_branches'];
$mmx_exclusive = $row['mmx_exclusive'];
$disclaimer = $row['disclaimer'];
$accre_shops = $row['accredited_shops'];
$roadside_assist_no = $row['roadside_assistance_number'];
$roadside_assist_cov = $row['roadside_assistance_coverage'];
$check_addressee = $row['check_addressee'];
$acc_details = $row['accounts_details'];
$can_charge = $row['cancellation_charge'];
echo '<h3>' .$prov .'</h3><p>'. $brok.'<br>'. $deduc.'<br>'. $deduc_waiver.'<br>'. $accre_banks.'<br>'. $mort.'<br>'. $casa_elig.'<br>'. $depre.'<br>'. $s_quotes.'<br>'. $age.'<br>'. $prov_info.'<br>'. $prov_branch.'<br>'. $mmx_exclusive.'<br>'. $disclaimer.'<br>'. $accre_shops.'<br>'. $roadside_assist_no.'<br>'. $roadside_assist_cov.'<br>'. $acc_details.'<br>'. $check_addressee.'<br>'. $can_charge.'</p><br />';
}
?>
</body>
</html>
2 जवाब
आप वास्तव में यह नहीं बता रहे हैं कि आपकी समस्या यहाँ क्या है। बस अपने कोड को देखते हुए, आप अपने डेटाबेस को अपनी mysqli क्वेरी में नहीं बुला रहे हैं, जो mysqli_query($db, "SELECT...") होना चाहिए, जहां $db आपका mysqli_connect वैरिएबल है। PHP मैनुअल में कुछ उदाहरण हैं: http://php.net/manual/ hi/mysqli.query.php.
जैसा कि मिकमैकुसा ने ऊपर भी कहा है, यदि आप बैकएंड पर "या डाई (mysqli_error($db)" जोड़ते हैं, तो यह आपको आपके sql सिंटैक्स के बारे में अधिक जानकारी देगा।
चेतावनी mysql_query, mysql_fetch_array, mysql_connect आदि.. एक्सटेंशन PHP 5.5.0 में बहिष्कृत थे, और इसे PHP 7.0.0 में हटा दिया गया था। इसके बजाय, MySQLi या PDO_MySQL एक्सटेंशन का उपयोग किया जाना चाहिए।
1) mysqli_*
या PDO
तैयार कथन का उपयोग करने का प्रयास करें
2) सबसे पहले मेरे सैंपल कोड को फॉलो करें
3) जहां से आप चर $q प्राप्त कर रहे हैं। आपको इस तरह $q =$_GET['q']; अन्यथा आपको $q undefined error
मिलेगा
//db connection
global $conn;
$servername = "localhost"; //host name
$username = "username"; //username
$password = "password"; //password
$mysql_database = "dbname"; //database name
//mysqli prepared statement
$conn = mysqli_connect($servername, $username, $password) or die("Connection failed: " . mysqli_connect_error());
mysqli_select_db($conn,$mysql_database) or die("Opps some thing went wrong");
$stmt = $conn->prepare("SELECT * FROM provider
WHERE provider LIKE %'".$q."'%
OR broker LIKE %'".$q."'%
OR deductible LIKE %'".$q."'%
OR deductible_waiver LIKE %'".$q."'%
OR accredited_banks LIKE %'".$q."'%
OR can_not_offer_mortgaged_with LIKE %'".$q."'%
OR casa_eligibility LIKE %'".$q."'%
OR depreciation LIKE %'".$q."'%
OR special_quotes LIKE %'".$q."'%
OR unit_age_limit LIKE %'".$q."'%
OR provider_information LIKE %'".$q."'%
OR provider_branches LIKE %'".$q."'%
OR mmx_exclusive LIKE %'".$q."'%
OR disclaimer LIKE %'".$q."'%
OR accredited_shops LIKE %'".$q."'%
OR roadside_assistance_number LIKE %'".$q."'%
OR roadside_assistance_coverage LIKE %'".$q."'%
OR check_addressee LIKE %'".$q."'%
OR accounts_details LIKE %'".$q."'%
OR cancellation_charge LIKE %'".$q."'%");
$stmt->execute();
$get_result =$stmt->get_result();
$row_count= $get_result->num_rows;
?>
<!DOCTYPE html>
<html>
<head>
<title> Search Form </title>
<link rel="stylesheet" type="text/css" href="css/stylesearch.css">
</head>
<body>
<form action = "search.php" method ="GET" id="searchForm">
<input type="text" name="q" id="searchBox" placeholder="Search Here" value="" /><input type="submit" id="searchBtn" value ="GO" />
</form>
<?php
if($row_count>0)
{
while($row = $get_result->fetch_assoc() )
{
$id = $row['id'];
$prov = $row['provider'];
$brok = $row['broker'];
$deduc = $row['deductible'];
$deduc_waiver = $row['deductible_waiver'];
$accre_banks = $row['accredited_banks'];
$mort = $row['can_not_offer_mortgaged_with'];
$casa_elig = $row['casa_eligibility'];
$depre = $row['depreciation'];
$s_quotes = $row['special_quotes'];
$age = $row['unit_age_limit'];
$prov_info = $row['provider_information'];
$prov_branch = $row['provider_branches'];
$mmx_exclusive = $row['mmx_exclusive'];
$disclaimer = $row['disclaimer'];
$accre_shops = $row['accredited_shops'];
$roadside_assist_no = $row['roadside_assistance_number'];
$roadside_assist_cov = $row['roadside_assistance_coverage'];
$check_addressee = $row['check_addressee'];
$acc_details = $row['accounts_details'];
$can_charge = $row['cancellation_charge'];
echo '<h3>' .$prov .'</h3><p>'. $brok.'<br>'. $deduc.'<br>'. $deduc_waiver.'<br>'. $accre_banks.'<br>'. $mort.'<br>'. $casa_elig.'<br>'. $depre.'<br>'. $s_quotes.'<br>'. $age.'<br>'. $prov_info.'<br>'. $prov_branch.'<br>'. $mmx_exclusive.'<br>'. $disclaimer.'<br>'. $accre_shops.'<br>'. $roadside_assist_no.'<br>'. $roadside_assist_cov.'<br>'. $acc_details.'<br>'. $check_addressee.'<br>'. $can_charge.'</p><br />';
}
}
$stmt->close();
$conn->close();
?>
</body>
</html>
संबंधित सवाल
नए सवाल
php
PHP एक व्यापक रूप से उपयोग किया जाता है, उच्च-स्तरीय, गतिशील, वस्तु-उन्मुख, और व्याख्या की गई स्क्रिप्टिंग भाषा मुख्य रूप से सर्वर-साइड वेब विकास के लिए डिज़ाइन की गई है। PHP भाषा के बारे में सवालों के लिए इस्तेमाल किया।
mysql
कार्यों को कूड़ेदान में फेंक कर शुरू करें।mysqli
याpdo
का प्रयोग करें। जब चीजें अपेक्षित रूप से काम नहीं करती हैं, तो mysqli_errors की जांच करें और/या अपनी जेनरेट की गई क्वेरी को सीधे अपने डेटाबेस में कॉपी करें और देखें कि यह परिणामसेट में क्या प्रदान करता है। साथ ही,LIKE
धीमा है, जो कॉलम आप खोज रहे हैं उन्हें सीमित करने का प्रयास करें। जैसे-जैसे आपकी डेटाबेस तालिका बढ़ती है, आपकी खोज धीमी गति से संसाधित होगी।mysql
फ़ंक्शंस बहिष्कृत हैं, और अब PHP7 में बिल्कुल भी काम नहीं करते हैं। मैं आपको पीडीओ के बारे में जानने की सलाह देता हूं, लेकिन आप कम से कम उन्हेंmysqli
फ़ंक्शन में अपग्रेड कर सकते हैं। दूसरा, आप कच्चे$_GET
डेटा को सीधे SQL स्टेटमेंट में पंप कर रहे हैं। तैयार किए गए कथनों और SQL इंजेक्शन हमलों से बचाव के तरीके के बारे में जानें। और, जैसा कि @mickmackusa सुझाव देता है, एक अलग वीडियो खोजें।